给动态id Angular2绑定 [英] Give dynamic id Angular2 Binds
本文介绍了给动态id Angular2绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JavaScript:
My JavaScript:
this.items = [
{name: 'Amsterdam1', id: '1'},
{name: 'Amsterdam2', id: '2'},
{name: 'Amsterdam3', id: '3'}
];
我的HTML:
<ul>
<li *ngFor="#item of items" id={{item.id}}>
{{ item.name}}
</li>
</ul>
我想为每个元素分配一个动态id,但无法让它工作。名称显示但id不是。
I want to assign a dynamic id to each element, but can't get it to work. The name is showing up but the id is not.
推荐答案
你说的方式有效,Angular 2.0.0.beta.8 。
the way you said works, Angular 2.0.0.beta.8.
在这种情况下你可以使用例如:
in this case you can use for example:
-
[id] =item.id
-
[attr.id] =item.id
-
id = {{item.id}}
<ul>
<li *ngFor="#item of items" #elem [id]="item.id">
{{ item.name}} ID: {{elem.id}}
</li>
</ul>
<ul>
<li *ngFor="#item of items" #elem [attr.id]="item.id">
{{ item.name}} ID: {{elem.id}}
</li>
</ul>
<ul>
<li *ngFor="#item of items" #elem id={{item.id}}>
{{ item.name}} ID: {{elem.id}}
</li>
</ul>
你可以在这里找有关模板语法的更多信息。
You can look here for more information in Template syntax.
https:// angular。 io / guide / cheatsheet
这篇关于给动态id Angular2绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文