Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set variable as html element attribute [duplicate]

Tags:

angular

I found a problem that I do not know how to solve.

If you use this template, so there is no problem:

<li *ngFor="#supplier of supplierList | async">
    <a (click)="changeSupplier($event)">
        <span>{{supplier.id}}: {{supplier.name}}</span>
    </a>
</li>

Output is e.g.:

<li>
    <a>
        <span>1: Sony</span>
    </a>
</li>
<li>
    <a>
        <span>2: Samsung</span>
    </a>
</li>

If I edit a template and try to set the "supplier.id" into html attribute "data-supplierid":

<li *ngFor="#supplier of supplierList | async">
    <a (click)="changeSupplier($event)" data-supplierid="{{supplier.id}}">
        <span>{{supplier.name}}</span>
    </a>
</li>

An error appears:

Can't bind to 'supplierid' since it isn't a known native property ("i>
                    <li *ngFor="#supplier of supplierList | async">
                        <a (click)="changeSupplier($event)" [ERROR ->]data-supplierid="{{supplier.id}}">
                            <span>{{supplier.name}}</span>
                        </a>
like image 479
JaSHin Avatar asked Dec 18 '22 20:12

JaSHin


1 Answers

Default is property binding. For attribute binding use either

attr.data-supplierid="{{supplier.id}}"

or

[attr.data-supplierid]="supplier.id"
like image 84
Günter Zöchbauer Avatar answered Dec 21 '22 10:12

Günter Zöchbauer