简体   繁体   English

是否有必要退订可观察的Angular Form statusChanges / valueChanges?

[英]Is it necessary to unsubscribe Angular Form statusChanges/valueChanges observable?

By dig into the angular form implementation I found the angular form valueChanges/statusChanges observable are EventEmitters. 通过深入研究角度形式实现,我发现可观察到的角度形式valueChanges / statusChanges是EventEmitters。 Is it necessary to unsubscribe these observables? 是否有必要取消订阅这些观测值? since when use EventEmitters alone we don't unsubscribe? 自从单独使用EventEmitters以来,我们不退订吗?

_initObservables() {
    (this as{valueChanges: Observable<any>}).valueChanges = new EventEmitter();
    (this as{statusChanges: Observable<any>}).statusChanges = new EventEmitter();
}

It is always a good practice to unsubscribe if notification is no longer required. 如果不再需要通知,则unsubscribe始终是一个好习惯。 Most of the time you can put into ngDestroy() function not necessarily. 在大多数情况下,您不一定需要将其放入ngDestroy()函数。

Is it always required to Unsubscribe ? 是否总是需要退订? No, If EventEmitter or Subject or Observable in same scope (Component or Service ) where Subscription is, then no need to destroy since all reference for all will be removed altogether. 否,如果EventEmitterSubjectObservableSubscription所在的同一范围内(Component或Service),则无需销毁,因为所有参考均将全部删除。

In short, make sure Observable is unsubscribe if scope are different for Subscription and Observable . 简而言之,如果SubscriptionObservable范围不同,请确保Observable unsubscribe Subscription

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

相关问题 Angular 10:我应该取消订阅 ngOnDestroy 中 valueChanges Observable 的订阅吗? - Angular 10 : Should I unsubscribe form subscription to valueChanges Observable in ngOnDestroy? Angular 2 - FormGroup ValueChanges 退订 - Angular 2 - FormGroup ValueChanges Unsubscribe 取消订阅 Angular valueChanges - Unsubscribe Angular valueChanges 当我在 valueChanges/statusChanges 中运行 setErrors 或 updateValueAndValidity 时,为什么 Angular 会将表单值重置为空 object? - Why does Angular reset the form value as an empty object, when I run setErrors or updateValueAndValidity inside a valueChanges/statusChanges? 当 updateOn 模糊时形成异步验证,以订阅 statusChanges 或 valueChanges - form async validation when updateOn is blur, to subscribe statusChanges or valueChanges Angular2取消订阅formGroup上的valueChanges - Angular2 unsubscribe valueChanges on formGroup 是否可以在不触发反应表单控件上的 valueChanges 的情况下触发 statusChanges? - Is it possible to trigger statusChanges without triggering valueChanges on a reactive form control? Angular2表单控件valueChanges可观察完成从未调用 - Angular2 form control valueChanges observable complete never called 如何取消订阅 Angular 4 反应形式的 valueChanges? - How to unsubscribe from valueChanges in Angular 4 reactive forms? 我应该取消订阅被动形式的valueChanges吗? - Should I Unsubscribe reactive form's valueChanges?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM