简体   繁体   中英

Angular 8: Have One Variable Subscribe to another variable in 1 Component class

I have two objects in Angular of the same type.

 public addressFinalData: AddressMailingData;
 mailingArchive: AddressMailingData[] = [];

How do I subscribe to data objects of the same type in 1 component

Want mailingArchive[2] to always subscribing to value of addressFinalData (anytime AddressFinalData changes, MailingArchive should store the value in array number 2).

You can create observable of AddressMailingData and in the subscription you can change with new value.

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.

this.addressFinalData.next({newData});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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