在Angular 4應用程序的組件中考慮以下簡單示例。它顯示了一個帶有兩個輸入字段的簡單HTML表單。一個輸入字段被直接執行,第二個是一個子組件內:如何在嵌套組件的表單中實現自動錶單驗證?
<form #personForm="ngForm">
<input type="text" required name="firstname [(ngModel)]="firstname"/>
<app-smart-input required [(model)]="lastname"></app-smart-input>
<button [disabled]="personForm.valid === false">Send</button>
</form>
分量被定義爲子如下:
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
@Component({
selector: "app-smart-input",
templateUrl: "./smart-input.component.html",
styleUrls: ["./smart-input.component.css"]
})
export class SmartInputComponent {
////////////////
// PROPERTIES //
////////////////
@Input() model: string;
@Output() modelChange: EventEmitter<string> = new EventEmitter<string>();
@Input("required") required: boolean = false;
/////////////
// METHODS //
/////////////
updateChanges() {
this.modelChange.emit(this.model);
}
}
與下面的HTML:
<input type="text" [required]="required" [(ngModel)]="model" (ngModelChange)="updateChanges()" />
現在更新模型完全正常(firstname
和lastname
由用戶輸入按預期定義)。
我想實現的是按鈕被禁用,除非填寫了兩個字段。請注意<input>
實現中的required
標誌,因此這些值不應爲null/undefined。
但不幸的是,該按鈕現在僅在firstname
未定義好的情況下被禁用。但表格並不在乎lastname
。
我該怎麼做到這一點?
注意:Angular 2 creating reactive forms with nested components是simular,但我使用模板驅動的形式,而不是反應形式。但它可能會以某種方式適應?