Modify value of html attributes with angular2

2

I want to change the value of the attributes according to the index but the page remains blank

<div *ngIf="detallePost">
    <div id="accordion" class="container">
        <h2>{{ detallePost.title | uppercase }}</h2>
        <div>
            <span>id del Post : </span>{{detallePost.id}}
        </div>
        <div>
            <span> {{ detallePost.body }}</span>
        </div>
        <ul>
            <li *ngFor="let comment of commentsPost; let i = index">
                <!-- Cabecera -->
                <div class="card">
                    <div class="card-header" id="heading{{i}}">
                        <h5 class="mb-0">
                            <button class="btn btn-link" 
                                    data-toggle="collapse"
                                    data-target="#collapse{{i}}"
                                    aria-expanded="true"
                                    aria-controls="collapse{{i}}">
                                    {{comment.email}}
                            </button>
                        </h5>
                    </div>
                    <!--Final Cabecera-->
                    <!--Texto mostrado-->
                    <div id="collapse{{i}}"
                         class="collapse" 
                         aria-labelledby="heading{{i}}" 
                         data-parent="#accordion">
                        <div class="card-body">
                                {{comment.body}}
                        </div>
                    </div>
                    <!--Final Texto mostrado-->
                </div>
            </li>
        </ul>
        <button (click)="goBack()" class="btn btn-primary">Volver</button>
    </div>
</div>

The error that the console gives is:

Uncaught Error: Template parse errors:
Can't bind to 'target' since it isn't a known property of 'button'. ("
<button class="btn btn-link" 
data-toggle="collapse"
[ERROR ->]data-target="#collapse{{i}}"
aria-expanded="true"
aria-controls="collapse{{i}}">
"): ng:///AppModule/DetallePostComponent.html@17:9
Can't bind to 'aria-controls' since it isn't a known property of 'button'. ("
    data-target="#collapse{{i}}"
    aria-expanded="true"
    [ERROR ->]aria-controls="collapse{{i}}">
    {{comment.email}}
    </button>
"): ng:///AppModule/DetallePostComponent.html@19:9
Can't bind to 'aria-labelledby' since it isn't a known property of 'div'. ("
    <div id="collapse{{i}}"
        class="collapse" 
        [ERROR ->]aria-labelledby="heading{{i}}" 
        data-parent="#accordion">
        <div class="card-body">
"): ng:///AppModule/DetallePostComponent.html@28:7
    at syntaxError (compiler.js:485)
    at TemplateParser.parse (compiler.js:24668)
    at JitCompiler._parseTemplate (compiler.js:34621)
    at JitCompiler._compileTemplate (compiler.js:34596)
    at eval (compiler.js:34497)
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:34497)
    at eval (compiler.js:34367)
    at Object.then (compiler.js:474)
    at JitCompiler._compileModuleAndComponents (compiler.js:34

The attributes that I want to modify are:

id="heading{{i}}
data-target="#collapse{{i}}"
aria-controls="collapse{{i}}">
id="collapse{{i}}"
aria-labelledby="heading{{i}}" 

Because I do not want them to repeat themselves, I have already verified that the {{i}} has values 0,1,2 ... etc

    
asked by EduBw 10.04.2018 в 09:08
source

1 answer

3

Angular distinguishes between attributes and properties. Generally, when you put an attribute on a tag in an Angular template, it assumes that it is a native property of HTML, a property of the "owner" component of the template or a directive. If you can not associate it with any of that, it fails.

So, how can you add attributes that are not properties, like the data-* ? As follows:

<button class="btn btn-link" 
  attr.data-toggle="collapse"
  attr.data-target="#collapse{{i}}"
  attr.aria-expanded="true"
  attr.aria-controls="collapse{{i}}">
  {{comment.email}}
</button>

That is, adding a attr. in front, to indicate to angular that they are pure attributes.

    
answered by 10.04.2018 / 10:12
source