如何将自定义组件注册为有效,无效,肮脏,被触摸等

时间:2018-12-23 04:41:26

标签: angular

如何将实现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>

1 个答案:

答案 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);
}