Ionic2 选择值未定义
Ionic2 selcted value undefined
你好,我有一个 select ionic 2 as 盒子。
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors()">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors(){
console.log(this.cities);
}
但是当我更改选项时它抛出一个错误,Error trying to diff '1'
谁能告诉我这里的问题
提前致谢
可能是因为您将 ngModel 命名为城市,而您的 *ngFor 也使用了城市。可能会引起冲突。我会将您的 ngModel 更改为
[(ngModel)]="selectedCity"
有两种方法可以获取所选项目的值。
方法一
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors($event){
console.log($event);
}
方法二
通过使用 ngModel
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="selectedcity" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
selectedcity: city
getSelectedCity(){
return this.selectedcity;
}
你好,我有一个 select ionic 2 as 盒子。
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors()">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors(){
console.log(this.cities);
}
但是当我更改选项时它抛出一个错误,Error trying to diff '1'
谁能告诉我这里的问题
提前致谢
可能是因为您将 ngModel 命名为城市,而您的 *ngFor 也使用了城市。可能会引起冲突。我会将您的 ngModel 更改为
[(ngModel)]="selectedCity"
有两种方法可以获取所选项目的值。
方法一
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors($event){
console.log($event);
}
方法二
通过使用 ngModel
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="selectedcity" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
selectedcity: city
getSelectedCity(){
return this.selectedcity;
}