您可以制作自己的组件。下面我提供一个例子(没有编译或运行它,但它应该给你足够的信息去实现)。只有在触摸,肮脏等情况下才显示消息的逻辑可以轻松添加到此处。
使用
<validation-messages [for]="control">
<validation-message name="required">This field is required</validation-message>
</validation-messages>
实施
import { Component, OnInit, ContentChildren, QueryList, Input, OnDestroy } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Subscription } from 'rxjs';
@Component({
selector: 'validation-messages',
template: '<ng-content></ng-content>'
})
export class ValidationMessagesComponent implements OnInit, OnDestroy {
@Input() for: FormControl;
@ContentChildren(ValidationMessageComponent) messageComponents: QueryList<ValidationMessageComponent>;
private statusChangesSubscription: Subscription;
ngOnInit() {
this.statusChangesSubscription = this.for.statusChanges.subscribe(x => {
this.messageComponents.forEach(messageComponent => messageComponent.show = false);
if (this.for.status === 'INVALID') {
let firstErrorMessageComponent = this.messageComponents.find(messageComponent => {
return messageComponent.showsErrorIncludedIn(Object.keys(this.for.errors));
});
firstErrorMessageComponent.show = true;
}
});
}
ngOnDestroy() {
this.statusChangesSubscription.unsubscribe();
}
}
@Component({
selector: 'validation-message',
template: '<div *ngIf="show"><ng-content></ng-content></div>'
})
export class ValidationMessageComponent {
@Input() name: string;
show: boolean = false;
showsErrorIncludedIn(errors: string[]): boolean {
return errors.some(error => error === this.name);
}
}
什么是'[供] = “控制”',其中'control'从何而来? – Felix
'control'是你想要在'ValidationMessagesComponent'中显示验证的'FormControl'。当使用[反应形式](https://angular.io/guide/reactive-forms)时,您可以自己创建这些实例。当使用[template-driven forms](https://angular.io/guide/forms)时,Angular本身会生成这些实例。我的解决方案对反应形式最好/最简单。 –
干得不错。稍微调整一下,对我来说完美无缺。 :) –