Angular 角度生命周期Hook ngOnChanges
每次变量在角度视图中更改后,我都要更新它Angular 角度生命周期Hook ngOnChanges,angular,lifecycle-hook,Angular,Lifecycle Hook,每次变量在角度视图中更改后,我都要更新它 如何在不使用@input装饰器的情况下以角度实现此功能?如果在组件中调用变量title,则可以执行以下操作: this.title.valueChanges .debounceTime(500) .distinctUntilChanged() .subscribe (newValue => { // do something based on the newvalue }); 但是
如何在不使用
@input
装饰器的情况下以角度实现此功能?如果在组件中调用变量title
,则可以执行以下操作:
this.title.valueChanges
.debounceTime(500)
.distinctUntilChanged()
.subscribe (newValue => {
// do something based on the newvalue
});
但是,现在您有了一个订户,它将在每次标题更改时触发。因此,您应该在某个时候将其销毁,否则将导致内存泄漏/堆栈溢出,例如:
this.title.valueChanges
.debounceTime(500)
.distinctUntilChanged()
.takeUntil(this.destroyValueChanges$)
.subscribe (newValue => {
// do something based on the newvalue
});
DelcaredestroyValueChanges$
作为组件的Subject
属性,并在Ngondestory
调用destroyValueChanges$.next()代码>
注意
因为你没有发布任何代码,我不得不猜你在问什么。我假设title是模板上的一个
,用户在其中键入,因此这就是值的变化方式。如果您正在讨论的变量在组件的“外部”被更改,那么它需要有一个@Input decorator,并且您需要使用ngOnChanges生命周期挂钩。您尝试过ngOnChanges事件吗?您尝试过什么?你能发布一些代码吗?我已经试过了,但是它不会更新。但是我没有使用@input decorator。为什么不使用ngModel
绑定来处理你的任何输入呢。