无法将变量设置为html元素属性

时间:2016-01-23 14:49:07

标签: angular

我发现了一个我不知道如何解决的问题。

如果您使用此模板,那么没有问题:

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

输出例如:

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

如果我编辑模板并尝试将“supplier.id”设置为html属性“data-supplierid”:

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

出现错误:

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>

1 个答案:

答案 0 :(得分:8)

默认为属性绑定。对于属性绑定,请使用

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

[attr.data-supplierid]="supplier.id"