2016-07-06 71 views
2

離子輸入值我有以下的HTML部分中ion-item創建動態基於*ngFor獲取動態創建的IONIC2

<ion-list *ngIf="Label_view"> 

    <ion-item style="Float:Right;margin-left: 4%;" *ngFor="let bil of arr_label;let i=index "> 
    <ion-label floating>{{bil}}</ion-label> 
    <ion-input [(ngModel)]="array" id={{i}} type="text" maxlength="5"></ion-input> 
    </ion-item> 

註冊

我得的ion-input值代入component。我已經使用[(ngModel)]值綁定到數組。

我的組件側

array:any[]=[]; 

Blr_regis() 
{ 
    console.log(array); 
    // console.log(document.getElementById("1").Value); 
    var x=document.getElementById("1").Value; 
    console.log(x); 
} 

我越來越不定位爲控制檯輸出。 有什麼遺漏嗎?

+1

中的代碼你得到了未定義,因爲你把一個數組作爲ngmodel。它應該是一個字符串或一個數字。像[(ngModel)] =「bil.Id」 – misha130

+0

謝謝.. :) @ misha130 –

回答

3

問題是因爲你試圖綁定一個數組與輸入元素。它應該綁定到一個字符串或一個數字(或陣列的單個位置)。

而不是做這樣的事情的:

<ion-item style="Float:Right;margin-left: 4%;" *ngFor="let bil of arr_label;let i=index "> 
    <ion-label floating>{{bil}}</ion-label> 
    <ion-input [(ngModel)]="array" id={{i}} type="text" maxlength="5"></ion-input> 
    </ion-item> 

然後,在該*ngFor這兩個let,你爲什麼不把所有的東西一起在同一陣列中是這樣的:

this.newArray : Array<{id: number, value: string}> = []; 

// Assuming the `arr_label` exists and it has been properly initialized 
for(let i=0; i < arr_label.length; i++) { 
    this.newArray.push({ id: i, value: arr_label[i] }); 
} 

而且那麼在你看來:

// We only iterate over the newArray because all the information we need is there 
    <ion-item style="Float:Right;margin-left: 4%;" *ngFor="let bill of newArray"> 

    <ion-label floating>{{bill.value}}</ion-label> 
    <ion-input [(ngModel)]="array[bill.id]" type="text" maxlength="5"></ion-input> 

    </ion-item> 

注意我們是現在使用id(這將是0,1等)將input綁定到陣列的單個位置。

你可以找到一個工作plunker here。看看Page1.tsPage1.html

相關問題