简体   繁体   English

如何在 Angular 中侦听组件中的服务变量更改

[英]How can I listen for a Service variable change in a component in Angular

I have a component with a button that triggers showSummary() when clicked that calls a service Appraisal-summary.service.ts that has a method calc()我有一个带有按钮的组件,该按钮在单击时触发showSummary()调用具有方法calc()的服务Appraisal-summary.service.ts

showSummary(appraisal) {
  this.summaryService.calc(appraisal);
}

with service Appraisal-summary.service.ts :使用服务Appraisal-summary.service.ts

calc(appraisal) {
  ...
  //a 'scores' array is created (synchronously)
  return this.scores;
}

How do I listen for the synchronous result this.scores to trigger a function in an unrelated component summary.component.ts (that has already been initialised) that will use scores .如何侦听同步结果this.scores以触发 function 在不相关的组件summary.component.ts (已初始化)将使用scores

something like: summary.component.ts :类似于: summary.component.ts

ngOnInit(): void {
  service.subscribe(scores => this.data = scores)
}

You would want to add a Subject inside your Appraisal-summary.service.ts like this:您可能希望在Appraisal-summary.service.ts中添加一个Subject ,如下所示:

import { Subject } from 'rxjs';

...
export class AppraisalSummaryService {
    // I am guessing scores is an array of type number
    public scoreSubject = new Subject<number[]>(); 
    
    calc(appraisal) {
      ...
      //a 'scores' array is created (synchronously)
      
      this.scoreSubject.next(this.scores); //emit the scores result
      return this.scores;
   }
}

And, in your other component, inside your ngOnInit , you want to listen to this result:而且,在您的其他组件中,在您的ngOnInit中,您想听这个结果:

import { Subscription } from 'rxjs';
....
export class YourOtherComponent implements OnInit, OnDestroy {
   private subscription: Subscription;

   constructor(private appraisalSummaryService: AppraisalSummaryService) {}

   public ngOnInit(): void {
     // you store your subscribe
     this.subscription = this.appraisalSummaryService.subscribe((scores: number[]) => {
       console.log(scores);
    });
   }
   
   public onDestroy(): void {
     // you need this in order to avoid a memory leak
     this.subscription.unsubscribe();
   }
   
}
```

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

相关问题 如何在Angular指令中监听元素的属性更改? - How can I listen an element's attribute change in an Angular directive? Angular服务变量更改并更新了另一个组件 - Angular service variable change and updated another component 如何将@Input中的变量传递给Angular2组件中的服务&gt; - How can I pass a variable from @Input to a service in an Angular2 component> 如何收听RactiveJS自定义组件? - How can I listen to a RactiveJS custom component? 如何在不重新加载整个列表的情况下在我的组件 CoinRow 中侦听此变量“编辑”的更改? - How can I listen for changes to this variable 'editing' in my component CoinRow without reload the entire list? 我该如何听角度2中的代码更改? - how can I listen to changes in code in angular 2? 如何使用自定义历史记录 object 在我的主 App 组件中收听路由更改? - How can I listen to route change in my main App component, using a custom history object? 如何在Ractive中收听属性更改 - How can I listen to an attribute change in Ractive 如何听 div 的高度变化? - How can I listen to the height change of a div? 在 Angular 7 中,当将数据从一个组件传递到另一个组件时,我是否使用服务并在接收组件中订阅/收听? - In Angular 7 when passing data from one component to another, do I use a service and subscribe/listen in the receiving component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM