繁体   English   中英

如何从组件中的方法创建和分配任意Angular2本地模板变量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM