[英]How to create and assign an arbitrary Angular2 local template variable from a method in my component
我正在寻找一种设置本地模板变量的方法,该变量的值由组件提供。
零件:
@Component({
'my-component
})
@View({
'templateUrl': '/templates/UserAnimals.html'
})
class UserAnimals {
users : Object[];
constructor() {
this.users = [
{ _id: 0, name: 'John' },
{ _id: 1, name: 'Mary' },
];
}
getFavorite(userId : string) : Object {
// return the animal object.
}
}
模板:
<table>
<tr *ngFor="#user of users"
favoriteAnimal="getFavorite(user._id)">
<!-- How do I do this? I'm looking for some way to set favoriteAnimal variable to
value from the component. After which favoriteAnimal is re-usable to all children
of the tr tag. -->
<td>
{{ user.name }}
</td>
<td>
{{ favoriteAnimal.type }}
</td>
<td>
{{ favoriteAnimal.name }}
</td>
</tr>
</table>
代替
{{ favoriteAnimal.type }}
和{{ favoriteAnimal.name }}
使用
{{getFavorite(user._id).type}}
和{{getFavorite(user._id).name}}
。
我不知道有什么方法可以像您描述的那样设置本地模板变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.