[英]Angular 8: Have One Variable Subscribe to another variable in 1 Component class
I have two objects in Angular of the same type.我在 Angular 中有两个相同类型的对象。
public addressFinalData: AddressMailingData;
mailingArchive: AddressMailingData[] = [];
How do I subscribe to data objects of the same type in 1 component如何在 1 个组件中订阅相同类型的数据对象
Want mailingArchive[2] to always subscribing to value of addressFinalData (anytime AddressFinalData changes, MailingArchive should store the value in array number 2).希望 mailingArchive[2] 始终订阅 addressFinalData 的值(任何时候 AddressFinalData 更改,MailingArchive 应该将值存储在数组 2 中)。
You can create observable of AddressMailingData and in the subscription you can change with new value.您可以创建 AddressMailingData 的 observable,并且在订阅中您可以使用新值进行更改。
public addressFinalData = new Subject<AddressMailingData>();
mailingArchive: AddressMailingData[] = [];
ngOnInit() {
this.addressFinalData.subscribe(
(val: any) => {
this.mailingArchive[2] = val;
}
)
}
So whenever you are changing the value of addressFinalData, you need to emit that data with next method.因此,无论何时更改 addressFinalData 的值,都需要使用 next 方法发出该数据。
this.addressFinalData.next({newData});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.