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