实际上我的情况是我想动态地改变数组循环。
export interface City {
street: Street[];
country: Country[];
}
<div>
<div (click)="OnClick()">Street</div>
<div (click)="OnClick()">Country</div>
</div>
<div *ngIf="clicked">
<div *ngFor="let c of city.street">
<div>
{{c.name}}
</div>
</div>
</div>
如果用户点击街道,街道的值应该循环。
预期:* ngFor =“let c of city.street”
如果用户点击国家/地区,则应循环播放该国家/地区的值。
预期:* ngFor =“let c of city.country”
我尝试了以下内容:
<div>
<div (click)="OnClick('street')">Street</div>
<div (click)="OnClick('country')">Country</div>
</div>
<div *ngIf="clicked">
//Porperty Binding
<div *ngFor="let c of city.{{onClickParameter}}">
<div>
{{c.name}}
</div>
</div>
</div>
它做得很好(模板分析错误因为城市。{{}}) 还有其他解决方案吗?
答案 0 :(得分:4)
您可以使用组件功能来处理它:
//Component
export interface City {
street: Street[];
country: Country[];
}
export class MyComponent {
public selected : string = 'street';
public city: City;
OnClick(select: string) {
this.selected = select;
}
}
// You HTML
<div>
<div (click)="OnClick('street')">Street</div>
<div (click)="OnClick('country')">Country</div>
</div>
<div *ngIf="clicked">
<div *ngFor="let c of city[selected]">
<div>{{c.name}}</div>
</div>
</div>