【发布时间】:2019-01-08 08:56:17
【问题描述】:
我已经编写了属性指令来显示错误类。
import { Directive, ElementRef, Renderer, Input, HostBinding } from '@angular/core';
import { FormControl } from '@angular/forms';
@Directive({
selector: "[appErrorClass]"
})
export class ErrorClasseDirective {
@Input('control') control: FormControl;
constructor(
private el: ElementRef,
private renderer: Renderer
) {
}
ngOnInit(): void {
console.log(this.control)
}
}
FormControl 来自控制器正在工作。但我无法在自定义指令中获取表单控件更新。我已经通过这样的控制了。
<div appErrorClass [control]="userForm.get('email')">
但是,我无法在指令中获得更新的 FormControl 状态。
请帮助任何人解决此问题。提前致谢。
【问题讨论】:
-
在 ngOnInit 中,
this.control.valueChanges.subscribe((value) => console.log(value))。请注意,当您的控件的组件被销毁时,您需要取消订阅。