简体   繁体   English

@input 在同一组件的不同实例中 angular 问题

[英]@input in different instances of same component angular problem

I have an angular 9 project and in this project there is a component which is a customized select option called custom-select-option.我有一个 angular 9 项目,在这个项目中有一个组件,它是一个名为 custom-select-option 的自定义 select 选项。 In this component i have an @input which is options of custom select. A select when selected should be hided from list.在这个组件中,我有一个@input,它是自定义 select 的选项。选择时应从列表中隐藏 select。 When i give same data retrieved from an API to multiple instances of this component when i select one option it hides from both instances, but it must hide from instance which clicked.当我将从 API 检索到的相同数据提供给该组件的多个实例时,当我 select 一个选项时,它从两个实例中隐藏,但它必须从单击的实例中隐藏。

in custom-select-option component:在自定义选择选项组件中:

  @Input() public data: customSelectOptionData[];
  onOptionSelected(text: string | number, value: any): void {
  this.isListOpen = false;
  this.titleSpan.nativeElement.innerHTML = text;
  if (this.controlName) this.inputHidden.nativeElement.value = value;
  if (this.parentFormGroup && this.controlName) 
  this.parentFormGroup.get(this.controlName).setValue(value)
  this.optionSelected.emit(String(value));
  this.data.forEach(item => {
  if (item.value === this.selectedOption.value) {
    item.hide = false;
  }
  if (item.value === value) {
    item.hide = true;
  }
  });
  this.selectedOption.text = text;
  this.selectedOption.value = value;

} }

in app component ts:在应用程序组件中:

  this.style.getAllStyles().pipe(take(1)).subscribe(data => {
  this.mainInstrumentStyles = [...data];
  this.secondInstrumentStyles = [...data];
  });

in app component html在应用组件 html

<custom-select-option [data]="mainInstrumentStyles]></custom-selec-option>
<custom-select-option [data]="secondInstrumentsStyle]></custom-selec-option>

how can i prevent these instances effect on each other input data?我怎样才能防止这些实例对彼此的输入数据产生影响?

Most probably you are sending references of the same object to multiple component instances.很可能您正在将同一个 object 的引用发送到多个组件实例。 Try to create a deep copy like following尝试像下面这样创建一个深拷贝

this.style.getAllStyles().pipe(take(1)).subscribe(data => {
  this.mainInstrumentStyles = JSON.parse(JSON.stringify([...data]));
  this.secondInstrumentStyles = JSON.parse(JSON.stringify([...data]));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular HostListener是同一组件的多个实例 - Angular HostListener multiple instances of same component Angular 1.6页面上相同组件的许多实例 - Angular 1.6 many instances of the same component on a page 如何在Angular 6中使用相同组件的多个实例? - How to use multiple instances of same component in Angular 6? React:componentDidUpdate对于组件的2个不同实例是否相同? - React: is componentDidUpdate same for 2 different instances of a component? Angular 组件多实例绑定输入问题 - Angular component multiple instance binding input problem Angular:同一页面上有多个 swiper slider 实例(已解决问题) - Angular: Multiple instances of swiper slider on same page (Solved problem) 迭代相同组件angular2的多个@ViewChild实例 - Iterate through multiple @ViewChild instances of the same component angular2 在同一个React组件的不同实例之间切换时使用componentDidMount - Utilizing componentDidMount when switching between different instances of the same React component 如何更新代表相同数据的两个不同的React组件实例? - How to update two different React component instances that represent the same data? 如何隔离同一组件的不同实例之间的js变量? - How to isolate js variables between different instances of the same component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM