json - 如何在模板中以角度绑定模型对象中的单个元素
问题描述
有人可以给我一个线索,如何从创建的 .model 绑定变量值?
我在 div 中使用了 ng for,然后我可以使用以下方法从 .model.ts 插入值:
ngFor let list of lists
then i {{name.list}}
但在其他 div 中,我只能显示 .model.ts 的动态名称,例如 name.list。
我怎样才能做到这一点?
谢谢
解决方案
如果您的组件中有一些数据,例如:
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>
推荐阅读
- traffic - 如何让firefox通过mitmproxy连接网页?
- javascript - PhpStorm:YUI Compressor JS 不适用于打字稿
- python - 如何在 Python 中使用 praw 将保存的 reddit 帖子列表抓取到 txt 文件
- python - 如何获取“src=...”的 url?
- machine-learning - 当我们在 PyTorch 张量上调用 cpu().data.numpy() 时会发生什么?
- android - 为什么我不能将数据放入 Firestore?
- powerbi - 使用 DAX 查找 MAX 7 天平均值
- xcode - 视图中缺少设备:在情节提要中
- android - 如何在 service worker 中播放声音甚至在 chrome 中振动?
- java - 如何在另一个布局中嵌入布局?