如何将实现ControlValueAccessor的组件注册为触摸,脏污,有效等
export class InputDropdownComponent implements ControlValueAccessor
我需要在模板中为自定义组件编写验证逻辑:
<div *ngIf="form.get('amount').hasError('outOfRange') && form.get('amount').touched"
class="validation-error">
The amount is out of range, minimum = ...
</div>
答案 0 :(得分:0)
ngControl具有设置表单属性的功能:
this.ngControl.control.markAsTouched();
this.ngControl.control.markAsDirty()
this.ngControl.control.markAsPristine()
要使用ngControl
:
import { NgControl } from "@angular/forms";
import { Injector } from "@angular/core";
constructor(private el: ElementRef, private inj: Injector) { }
ngOnInit() {
this.ngControl = this.inj.get(NgControl);
}