angular 2 : ngfor 复杂对象
angular 2 : ngfor complex object
我在使用 ngofr
在 angular 2 中显示复杂对象时遇到问题
jobs = [
new Job('MacGronalds', [
new Position("Sweeper"),
new Position("burger flipper")
]),
new Job('Starduck', [
new Position("Sweeper"),
new Position("burger flipper")
])
];
我怎样才能像这样遍历我的对象?
<li *ngFor="let job of jobs">
{{ job.companyName }}
<div *ngFor="let position of job">
{{position.name}}
</div>
</li>
谢谢!
假设 "Positions" 是名为 "positions" 的 属性 中的一个数组,您需要做的就是:
<div *ngFor="let position of job.positions">
您的模型应该如下所示。
export class Job{
constructor(
public companyName : string,
public positions: Position[]
) { }
}
export class Position{
constructor(
public name: string
) { }
}
并且您有一个存储作业的字段,例如 private jobs : Job[];
然后你可以用下面的循环
<li *ngFor="let job of jobs">
{{ job.companyName }}
<div *ngFor="let position of job.positions">
{{position.name}}
</div>
</li>
我在使用 ngofr
在 angular 2 中显示复杂对象时遇到问题jobs = [
new Job('MacGronalds', [
new Position("Sweeper"),
new Position("burger flipper")
]),
new Job('Starduck', [
new Position("Sweeper"),
new Position("burger flipper")
])
];
我怎样才能像这样遍历我的对象?
<li *ngFor="let job of jobs">
{{ job.companyName }}
<div *ngFor="let position of job">
{{position.name}}
</div>
</li>
谢谢!
假设 "Positions" 是名为 "positions" 的 属性 中的一个数组,您需要做的就是:
<div *ngFor="let position of job.positions">
您的模型应该如下所示。
export class Job{
constructor(
public companyName : string,
public positions: Position[]
) { }
}
export class Position{
constructor(
public name: string
) { }
}
并且您有一个存储作业的字段,例如 private jobs : Job[];
然后你可以用下面的循环
<li *ngFor="let job of jobs">
{{ job.companyName }}
<div *ngFor="let position of job.positions">
{{position.name}}
</div>
</li>