我上的角2項目中,我就按一下按鈕動態地創建輸入領域的工作。我正在接受FormArray的幫助。字段和數據提交的創建工作正常,但是當我試圖用一些預定義的數據填充字段時,它不起作用。我爲我的問題創造了一個蹲點。 https://plnkr.co/edit/PCFD43GK91zo2ivQ9lf7?p=preview 這裏我想用對象itemData中的數據填充視圖上的字段。爲了便於參考,以下是角碼 -用數據填充的動態表單控件角2
//our root app component
import { Component, NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormBuilder, FormGroup, Validators, FormArray, FormControl } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `<hr>
<div>
<form [formGroup]="orderForm" (ngSubmit)="OnSubmit(orderForm.value)">
<div>
<div>
<label>Customer Name</label>
<input type="text" formControlName="customerName"/>
<small *ngIf="IsValidField('customerName')" class="text-danger">
Customer Name is required
</small>
</div>
<br/>
<div>
<label>Customer Email</label>
<input type="text" formControlName="email"/>
<small *ngIf="IsValidField('email')" class="text-danger">
Email is required
</small>
</div>
</div>
<br/>
<div formArrayName="items" *ngFor="let item of items.controls; let i = index;">
<div [formGroupName]="i">
<div>
<label>Item Name</label>
<input type="text" formControlName="name" placeholder="Item name"/>
<small *ngIf="IsValidField('name',i)" class="text-danger">
Item Name is required
</small>
</div>
<br/>
<div>
<label>Item Description</label>
<input type="text" formControlName="description" placeholder="Item description"/>
<small *ngIf="IsValidField('description',i)" class="text-danger">
Description is required
</small>
</div>
<br/>
<div>
<label>Item Price</label>
<input type="text" formControlName="price" placeholder="Item price"/>
<small *ngIf="IsValidField('price',i)" class="text-danger">
Price is required
</small>
</div>
<br/>
</div>
</div>
<button type="submit">Save</button>
<button type="button" (click)="addItem()">Add More</button>
<button type="button" (click)="loadItems()">Load Items</button>
</form>
<div>`,
})
export class App {
constructor(private formBuilder: FormBuilder) { }
public orderForm: FormGroup;
public formSubmitAttempt: boolean;
public itemData:any=`{
"customerName":"Customer 1","email":"[email protected]",
"items":[{"name":"Item 1","description":"Item 1 Descr","price":"100"},
{"name":"Item 2","description":"Item 2 Descr","price":"200"},
{"name":"Item 3","description":"Item 3 Descr","price":"300"}]
}`;
ngOnInit() {
this.orderForm = this.formBuilder.group({
customerName: ['',[Validators.required]],
email: ['',[Validators.required]],
items: this.formBuilder.array([ this.createItem()])
});
}
createItem(): FormGroup {
return this.formBuilder.group({
name: ['',[Validators.required,Validators.maxLength(10)]],
description: '',
price: ['',[Validators.required,Validators.pattern("[(0-9)]*")]]
});
}
public loadItems(){
this.orderForm = this.formBuilder.group({
customerName: [this.itemData.customerName,[Validators.required]],
email: [this.itemData.email,[Validators.required]],
items: this.itemData.items
});
}
get items(): FormArray {
return this.orderForm.get('items') as FormArray;
};
addItem(): void {
this.items.push(this.createItem());
}
public OnSubmit(formValue: any) {
this.formSubmitAttempt = true;
console.log(JSON.stringify(formValue));
}
public IsValidField(field: string, i?:number) {
if(i!=null) {
var f = this.orderForm
.get('items') //retrieve items FormArray
.get(i.toString()) //retrieve items FormGroup
.get(field); //retrieve items form field
return (f.invalid && f.touched) || (f.invalid && this.formSubmitAttempt);
} else {
return (this.orderForm.get(field).invalid && this.orderForm.get(field).touched) || (this.orderForm.get(field).invalid && this.formSubmitAttempt);
}
}
}
@NgModule({
imports: [ BrowserModule, FormsModule, ReactiveFormsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
任何幫助,將不勝感激。謝謝。
請您詳細說明一下。我試圖像這樣 公共loadItems(){ this.orderForm = this.formBuilder.group({ 客戶名稱:[this.itemData.customerName,[Validators.required], 電子郵件:[this.itemData.email ,[Validators.required]], 項目:this.formBuilder.array([this.itemData.items]) }); } 但它沒有奏效。 [plunker](https://plnkr.co/edit/PCFD43GK91zo2ivQ9lf7?p=preview) 它拋出以下錯誤 無法找到控制路徑:'項目 - > 0 - >名稱' –
你剛剛複製了該帖子中的內容,當然不會工作,因爲formArray需要formGroups,this.itemData.items只是一個對象 – Sonicd300
非常感謝,它確實有幫助。它的工作現在。唯一的問題是數據填充時,以前的控件(在init中默認創建)也保留在那裏。任何解決此問題的方法? [更新Plunker(https://plnkr.co/edit/PCFD43GK91zo2ivQ9lf7?p=preview) –