简体   繁体   English

Angular 8:有一个变量订阅 1 个组件中的另一个变量 class

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

相关问题 在订阅响应中初始化的类型为Item的Angular 4 Component变量没有在Item类中定义的方法 - Angular 4 Component variable of type Item initialized in subscribe response does not have methods defined in the Item class 一个函数中设置的角组件类变量在另一个函数中未定义 - Angular Component Class Variable Set in one Function is Undefined in Another Angular 2 将变量从一个组件传递到另一个组件 - Angular 2 pass variable from one component to another Angular Http 订阅 - 无法分配给组件中的变量 - Angular Http subscribe - cannot assign to variable in component 将数据通过一个组件传递到另一个具有 Angular 材料表的组件的问题 - 未解决的变量 - Problem with passing data trough one component to another that have a Angular Material Table - Unresolved variable 角度如何从可观察订阅的结果中设置组件变量 - angular how to set a component variable from result in observable subscribe 如何创建一个具有变量的类,该变量可以具有两种角度之一 - how to create a class with a variable that could have one of two types angular 修改另一个组件的组件类变量 - Modify a component class variable from another component 在订阅之外未定义Angular 4变量 - Angular 4 variable is undefined outside subscribe PhpStorm Angular订阅-未解析的变量 - PhpStorm Angular subscribe - unresolved variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM