![](/img/trans.png)
[英]Angular FormControl not detecting changes made by input directive
[英]HostBinding a FormControl to a input within a Directive
我無法通過附加到輸入的指令內的 HostBinding 將 formControl 添加到輸入。 請讓我知道這是否是一種可能的方法,如果可以,該怎么做。
輸入
<input matInput 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);
});
}
}
要訪問 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.