![](/img/trans.png)
[英]Angular 2 how to call markAsDirty() on control inside ControlValueAccessor
[英]How to access 'updateOn' option inside custom implementation of ControlValueAccessor in Angular?
我必须遵循表格
form = new FormGroup({
title: new FormControl(null, {updateOn: 'blur'}),
})
对于标题输入,我创建了一个实现ControlValueAccessor
的自定义控件title-input
。 如何访问此控件中的updateOn
选项?
经过一番谷歌搜索,我找到了两个解决方案:
请注意,没有NG_VALUE_ACCESSOR
提供程序。
@Component({
selector: 'app-my-input',
templateUrl: './my-input.component.html',
styles: [],
})
export class MyInputComponent implements OnInit, ControlValueAccessor, AfterViewInit {
onChange: (_: any) => void;
onTouched: () => void;
isDisabled: boolean;
value: string;
constructor(private ngControl: NgControl) {
ngControl.valueAccessor = this;
}
ngOnInit() {
console.log('Update On: ', this.ngControl.control.updateOn);
}
}
@Component({
selector: 'app-my-input',
templateUrl: './my-input.component.html',
styles: [],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyInputComponent),
multi: true,
},
],
})
export class MyInputComponent implements OnInit, ControlValueAccessor, AfterViewInit {
onChange: (_: any) => void;
onTouched: () => void;
isDisabled: boolean;
value: string;
ngControl: NgControl;
constructor(private inj: Injector) {
}
ngOnInit() {
this.ngControl = this.inj.get(NgControl);
}
ngAfterViewInit(): void {
console.log('Update on:', this.ngControl.control.updateOn);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.