我正在寻找一种设置本地模板变量的方法,该变量的值由组件提供。
零件:
@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>