我想在Angular 2中使用*ngFor迭代[object object].
问题是对象不是对象数组而是包含更多对象的对象对象.
{
"data": {
"id": 834,
"first_name": "GS",
"last_name": "Shahid",
"phone": "03215110224",
"role": null,
"email": "test@example.com",
"picture": **{ <-- I want to get thumb: url but not able to fetch that**
"url": null,
"thumb": {
"url": null
}
},
"address": "Nishtar Colony",
"city_id": 2,
"provider": "email",
"uid": "test@example.com"
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我们可以使用管道迭代对象,但我们如何能够从对象进一步迭代意味着data-> picture-> thum:url.
Gün*_*uer 134
Angular 6.0.0
https://github.com/angular/angular/blob/master/CHANGELOG.md#610-beta1-2018-06-13
介绍一个 KeyValuePipe
Run Code Online (Sandbox Code Playgroud)@Component({ selector: 'keyvalue-pipe', template: `<span> <p>Object</p> <div *ngFor="let item of object | keyvalue"> {{item.key}}:{{item.value}} </div> <p>Map</p> <div *ngFor="let item of map | keyvalue"> {{item.key}}:{{item.value}} </div> </span>` }) export class KeyValuePipeComponent { object: {[key: number]: string} = {2: 'foo', 1: 'bar'}; map = new Map([[2, 'foo'], [1, 'bar']]); }
原版的
你可以使用管道
@Pipe({ name: 'keys', pure: false })
export class KeysPipe implements PipeTransform {
transform(value: any, args: any[] = null): any {
return Object.keys(value)//.map(key => value[key]);
}
}
Run Code Online (Sandbox Code Playgroud)
<div *ngFor="let key of objs | keys">
Run Code Online (Sandbox Code Playgroud)
另请参见如何使用*ngFor迭代对象键?
She*_*eki 36
我认为最优雅的方法是使用这样的javascript Object.keys:
在Component中将Object传递给模板:
Object = Object;
Run Code Online (Sandbox Code Playgroud)
然后在模板中:
<div *ngFor="let key of Object.keys(objs)">
my key: {{key}}
my object {{objs[key] | json}} <!-- hier I could use ngFor again with Object.keys(objs[key]) -->
</div>
Run Code Online (Sandbox Code Playgroud)
afs*_*182 12
循环遍历 Angular HTML 模板中的对象
Angular中引入keyValuePipe:请参考https://angular.io/api/common/KeyValuePipe
快速代码:
<p>Object</p>
<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>
<p>Map</p>
<div *ngFor="let item of map | keyvalue">
{{item.key}}:{{item.value}}
</div>
Run Code Online (Sandbox Code Playgroud)
您必须创建自定义管道.
import { Injectable, Pipe } from '@angular/core';
@Pipe({
name: 'keyobject'
})
@Injectable()
export class Keyobject {
transform(value, args:string[]):any {
let keys = [];
for (let key in value) {
keys.push({key: key, value: value[key]});
}
return keys;
}}
Run Code Online (Sandbox Code Playgroud)
然后在你的*ngFor中使用它
*ngFor="let item of data | keyobject"
Run Code Online (Sandbox Code Playgroud)
我知道这个问题已经回答,但是我有一个解决方案。
您还可以Object.keys()在中使用*ngFor以获得所需的结果。
我已经在stackblitz上创建了一个演示。我希望这会对您/其他人有所帮助/指导。
代码片段
HTML代码
<div *ngFor="let key of Object.keys(myObj)">
<p>Key-> {{key}} and value is -> {{myObj[key]}}</p>
</div>
Run Code Online (Sandbox Code Playgroud)
.ts文件代码
Object = Object;
myObj = {
"id": 834,
"first_name": "GS",
"last_name": "Shahid",
"phone": "1234567890",
"role": null,
"email": "test@example.com",
"picture": {
"url": null,
"thumb": {
"url": null
}
},
"address": "XYZ Colony",
"city_id": 2,
"provider": "email",
"uid": "test@example.com"
}
Run Code Online (Sandbox Code Playgroud)
1.创建自定义管道来获取密钥。
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'keys'
})
export class KeysPipe implements PipeTransform {
transform(value: any, args?: any): any {
return Object.keys(value);
}
}
Run Code Online (Sandbox Code Playgroud)
- 在角度模板文件中,您可以使用 *ngFor 并迭代您的对象对象
<div class ="test" *ngFor="let key of Obj | keys">
{{key}}
{{Obj[key].property}
<div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97530 次 |
| 最近记录: |