Angular Refresh选择选项

发布于 2025-01-27 03:23:54 字数 626 浏览 3 评论 0原文

选择一个更改后,我想刷新选择B选项 我该怎么做?

//select A
<select (change)="fasechange()" name="" [(ngModel)]="fase" id="">
   <option *ngFor="let item of arrayA">{{item.name}}</option>
</select>

//Select B
<select name="" [(ngModel)]="equipo1"  id="">
    <option *ngFor="let item of arrayB">{{item}}</option>
</select>

在选择A NED更新的更改函数中

arrayB: Array<string> = [];
fasechange(){
  for(let a of this.options){
    if(a.name == this.fase){
      this.arrayB.push(this.option.opt);
    }
  }
}

,刷新选择b的变化功能结束,我该如何实现??? 感谢您的帮助

i want to refresh the select B options once select A change
how can i made this?

//select A
<select (change)="fasechange()" name="" [(ngModel)]="fase" id="">
   <option *ngFor="let item of arrayA">{{item.name}}</option>
</select>

//Select B
<select name="" [(ngModel)]="equipo1"  id="">
    <option *ngFor="let item of arrayB">{{item}}</option>
</select>

and in the change function for select A

arrayB: Array<string> = [];
fasechange(){
  for(let a of this.options){
    if(a.name == this.fase){
      this.arrayB.push(this.option.opt);
    }
  }
}

i ned to update, refresh the select B onnce the change function ends, how can i make this happen???
thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱暴击 2025-02-03 03:23:54

您也可以使用 changeTectorRef 强制更新。

fasechange(){
  for(let a of this.options){
    if(a.name == this.fase){
      this.arrayB.push(this.option.opt);
      this.changeDetector.detectChanges(); // Inject it in your component constructor
    }
  }
}

You could also use ChangeDetectorRef to force the update.

fasechange(){
  for(let a of this.options){
    if(a.name == this.fase){
      this.arrayB.push(this.option.opt);
      this.changeDetector.detectChanges(); // Inject it in your component constructor
    }
  }
}
方觉久 2025-02-03 03:23:54

按下后,您需要为ArrayB创建新的参考。

fasechange(){
  for(let a of this.options){
      if(a.name == this.fase){
          this.arrayB.push(this.option.opt);
          this.arrayB  = [...this.arrayB]
      }
   }
}

After Push, you need to create new reference for arrayB.

fasechange(){
  for(let a of this.options){
      if(a.name == this.fase){
          this.arrayB.push(this.option.opt);
          this.arrayB  = [...this.arrayB]
      }
   }
}
全部不再 2025-02-03 03:23:54

推动后,您需要重新创建数组参考(如此答案)也克隆对象。

因此,代码将是这样的:

this.arrayB = JSON.parse(JSON.stringify(this.arrayB));

或使用Lodash(或任何其他具有深克隆的公用事业库:

this.arrayB = _.cloneDeep(this.arrayB);

After push you need to recreate the array reference (as mentioned in this answer) but since you have array of objects, you need to clone the objects as well.

So the code would be something like this:

this.arrayB = JSON.parse(JSON.stringify(this.arrayB));

or with lodash (or any other utility library with deep cloning:

this.arrayB = _.cloneDeep(this.arrayB);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文