4

我有以下 HTML 部分,其中ion-item 基于动态创建*ngFor

<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.I 已使用[(ngModel)]值绑定到数组。

我的组件端

array:any[]=[];

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

我将 UNDEFINED 作为控制台输出。有什么遗漏吗?

4

1 回答 1

4

问题是因为您试图将 Array 与输入元素绑定。它应该绑定到字符串或数字(或数组的单个位置)。

而不是做这样的事情:

  <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>

并且将这两个let放在 中*ngFor,为什么不将所有内容放在同一个数组中,如下所示:

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>

请注意,我们现在使用(将是 0、1 等)将绑定input到数组的单个位置。id

你可以在这里找到一个工作的plunker。看看里面的代码Page1.tsPage1.html

于 2016-07-06T06:16:31.727 回答