[英]Dynamic Change *ngIf State with Function Angular
<ion-col sizeLg="4" size="12" *ngFor="let item of category.menu_items">
<ion-badge (click)="follow(item)" *ngIf="followcontrol(item.id)">Follow</ion-badge>
</ion-col>
當頁面打開時它工作正常。 當點擊這個按鈕時followcontrol()
function 結果會改變。 我的follow()
function:
follow(item) {
this.request.postFollowed(item)
this.following(); // This is for succesfull message
}
followcontrol()
:
followcontrol(id){
return this.request.getFollowByID(this.followsdata, id) // this function control the data and return a boolean value.
}
在follow()
function 工作后,如何獲取followcontrol()
的當前值?
將 id 保存到數組 (ids) 中:
ids: number[] = [];
follows: boolean[] = [];
ngOnInit() {
this.your_service.getCategory().subscribe(category => {
category.menu_items.forEach((item, index) => {
this.ids[index] = item.id;
this.followcontrol(item.id, index);
}
});
}
followcontrol(id, index) {
this.request.getFollowById(this.followsdata, id).subscribe(value => {
this.follows[index] = value;
});
}
在你的模板中:
<ion-col sizeLg="4" size="12" *ngFor="let item of category.menu_items; let i = index">
<ion-badge (click)="follow(item)" *ngIf="follows[i]">Follow</ion-badge>
</ion-col>
因此,您將避免在模板內部使用方法(這會產生副作用),並且您已准備好初始化中的所有數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.