How to iterate through an Object attributes in Angular 2(如何遍历Angular 2中的对象属性)
问题描述
这是我的对象(它有 n 个动态键.我在下面的示例中只显示了两个)
Here's my object (It has n number of dynamic keys. I've only shown two in the example below)
let obj = {
abc:["some text", "some more text"],
xyz:["more text", "what do you think?", "I'm tired now"]
}
这是我尝试循环抛出上述内容并打印所有值
Here's my attempt to loop throw the above and print all the values
<div *ngFor='let item of obj ; let i = index;'>
<p *ngFor="let value of obj.i">{{value}}
</div>
但上述方法似乎不起作用.我做错了什么,正确的语法是什么?
But the above doesn't appear to work. What am I doing wrong and what's the correct syntax?
推荐答案
你可以这样做:
<li *ngFor="let o of obj">
<p *ngFor="let objArrayElement of generateArray(o)"> {{objArrayElement}} </p>
</li>
其中 generateArray
看起来像:
generateArray(obj){
return Object.keys(obj).map((key)=>{ return obj[key]});
}
这篇关于如何遍历Angular 2中的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何遍历Angular 2中的对象属性


基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01