2017-02-15 27 views
0

使用ng-model我想將字段與數組對象綁定在一起,所以無論何時checkbox被選中或input字段都有一些值,它會自動填充到數組對象中。綁定到該字段的ng模型不起作用

以下是我目前使用的代碼。請告知我做錯了什麼。

home.ts

export class HomePage { 

    extras: any; 
    enhancements: any; 

    constructor(public navCtrl: NavController, public http: Http) { 
    this.http.get('https://www.example.com/api/enhance/11/?format=json').map(res => res.json()).subscribe(response => { 
     this.extras = response.Extras; 
     this.enhancements = {}; 
     this.extras.forEach(item => { 
      this.enhancements[item.id] = { checked: false, qty: 0 }; 
     }) 
    }); 
    } 

    onChange(){ 
    console.log(this.enhancements); 
    } 

} 

home.html的

<ion-content padding> 
    <ion-grid> 
     <ion-row *ngFor="let item of extras" id="booking-enhancements-wrap-{{ item.id }}"> 
      <ion-col width-10> 
       <ion-checkbox (ionChange)="onChange()" ng-model="enhancements[item.id].checked" ng-checked="enhancements[item.id].checked"></ion-checkbox> 
      </ion-col> 
      <ion-col width-70>{{ item.name }}</ion-col> 
      <ion-col width-20><input type="number " id="qty-{{ item.id }} " style="width: 100%; " (input)="onChange()" ng-model="enhancements[item.id].qty" /></ion-col> 
     </ion-row>= 
    </ion-grid> 
</ion-content> 

回答

1

試試這個 更換

this.enhancements = {}; 

this.enhancements = [];