首页 > 解决方案 > 如何在模板中以角度绑定模型对象中的单个元素

问题描述

有人可以给我一个线索,如何从创建的 .model 绑定变量值?

我在 div 中使用了 ng for,然后我可以使用以下方法从 .model.ts 插入值:

ngFor let list of lists

then i {{name.list}}

但在其他 div 中,我只能显示 .model.ts 的动态名称,例如 name.list。

我怎样才能做到这一点?

谢谢

标签: jsonangular

解决方案


如果您的组件中有一些数据,例如:

  usersList = [
    { name: "Michael", id: 1 },
    { name: "Linda", id: 2 }
  ];

在您的模板 html 中,您应该执行以下操作:

<h2>List of users</h2>
<ul *ngIf="userList.length">
  <li *ngFor="let user of userList">
    Hello {{user.name}} (id: {{user.id}})
  </li>
</ul>
<div *ngIf="!userList.length">No users</div>

推荐阅读