您可以製作自己的組件。下面我提供一個例子(沒有編譯或運行它,但它應該給你足夠的信息去實現)。只有在觸摸,骯髒等情況下才顯示消息的邏輯可以輕鬆添加到此處。
使用
<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本身會生成這些實例。我的解決方案對反應形式最好/最簡單。 –
幹得不錯。稍微調整一下,對我來說完美無缺。 :) –