繁体   English   中英

动态变化 *ngIf State 和 Function Angular

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM