我試圖設置一個表單,管理員用戶可以設置氣瓶的價格。一瓶有一個類型,名稱,價格如下:Angular2:無法獲取NGFFor在ngForm中重複輸入的輸入值
export class Bottle {
constructor(
public typeId: string,
public name: string,
public price: number
)
{ }
}
這裏顯示的形式:一個帳戶ID輸入,並與ngFor重複瓶子的列表。
<form (ngSubmit)="onSubmit()" #bottleUpdatePriceForm="ngForm" >
<div class="form-group">
<label for="accountId ">Account ID : </label>
<input type="text" class="form-control" id="accoundId" name="accountId" required [(ngModel)]="accountId">
</div>
<div class="form-group" *ngFor="let bottle of bottleArrayToUpdate; let i = index">
<label for="bottlePrice ">{{bottle.name}} : </label>
<input type="text" class="form-control" id="bottlePrice" name="bottlePrice" [ngModel]="bottleArrayToUpdate[i].price">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
我從另一個組件作爲瓶(6實際上),他們的類型和名稱設置陣列發送數據到我的形式,並且其中價格爲空。
@Input() private bottleArrayToUpdate: Bottle[];
...
onSubmit() {
this.submitted = true;
console.log(this.accountId); // => Works fine
console.log(this.bottleArrayToUpdate); // => All the prices are still null
}
有沒有辦法讓瓶子的重複輸入的輸入值和標準形式,而不是反應形式?
我也試過[ngModel]="bottle.price
但我無法訪問我的值。
感謝您的幫助
此外,可能是一個可能的重複,但我沒有找到答案在其他職位,因此我的問題。 –