[英]HostBinding a FormControl to a input within a Directive
I am having trouble adding a formControl to a input via a HostBinding inside a directive attached to the Input.我无法通过附加到输入的指令内的 HostBinding 将 formControl 添加到输入。 Please let me know if this is a possible approach and if so how to do it.
请让我知道这是否是一种可能的方法,如果可以,该怎么做。
Input输入
<input matInput searchInput>
The Directive (searchInput)指令 (searchInput)
@Directive({
selector: '[searchInput]',
})
export class SearchableSelectDirective implements AfterViewInit {
@HostBinding('attr.[formControl]') control: FormControl = new FormControl('');
ngAfterViewInit(): void {
this.sub = this.control.valueChanges.subscribe((value: string) => {
console.log(value);
});
}
}
To access the FormControl reference you need to use NgControl
要访问 FormControl 参考,您需要使用
NgControl
@Directive({
selector: '[searchInput]',
})
export class SearchableSelectDirective implements AfterViewInit {
sub: any;
constructor(private ngControl: NgControl) {}
ngAfterViewInit(): void {
this.sub = this.ngControl.valueChanges.subscribe((value: string) => {
console.log(value);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.