Ionic3 - 如何更新动态 ngModel 的值?
Ionic3 - How to update value of dynamic ngModel?
我正在使用 ionic 3 框架。如何更改 ngModel 的值?我想以编程方式切换所有离子切换。
分量:
allRecs:any;
constructor(){
this.allRecs = [
{
label: "label 1",
model : "model1"
},
{
label: "label 2",
model : "model2"
},
{
label: "label 3",
model : "model3"
}
]
}
public toggle(flag:boolean){
console.log(flag);
}
html:
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.model" (ionChange)="toggle(x.model)" item-end>
</ion-toggle>
</ion-item>
有没有人有想法?
ion-toggle 需要一个布尔值,如果你将它绑定到一个布尔值,它就会起作用。
在您的 allRecs 模型属性中是字符串,因此初始值不会影响离子切换并且无法更改它。所以 x.model 应该是布尔值或为例如值添加一个新的布尔属性以将其设置为 ngModel:
constructor(){
this.allRecs = [
{
label: "label 1",
value: false
},
{
label: "label 2",
value: false
},
{
label: "label 3",
value: true
}
]
}
toggle(flag:boolean){
for(let i=0;i<this.allRecs.length;i++){
this.allRecs[i].value = flag;
}
}
在html中:
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.value" (ionChange)="toggle(x.value)" item-end>
</ion-toggle>
</ion-item>
我试着像上面的例子那样做,但需要像下面这样的一些改进。
constructor(){
this.allRecs = [
{
id: 1, //add this line
label: "label 1",
value: false
},
{
id: 2, //add this line
label: "label 2",
value: false
},
{
id: 3, //add this line
label: "label 3",
value: true
}
]
}
/*
* in this method added new parameter `id: number`
*/
toggle(id: number, flag:boolean) {
for(let i=0;i<this.allRecs.length;i++) {
//check if the current record has the same id
if (this.allRecs[i].id == id) {
this.allRecs[i].value = flag;
}
}
}
在html中:
<!-- added new parameter `x.id` when occurs `ionChange` event calling toggle method -->
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.value" (ionChange)="toggle(x.id, x.value)" item-end>
</ion-toggle>
</ion-item>
我正在使用 ionic 3 框架。如何更改 ngModel 的值?我想以编程方式切换所有离子切换。
分量:
allRecs:any;
constructor(){
this.allRecs = [
{
label: "label 1",
model : "model1"
},
{
label: "label 2",
model : "model2"
},
{
label: "label 3",
model : "model3"
}
]
}
public toggle(flag:boolean){
console.log(flag);
}
html:
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.model" (ionChange)="toggle(x.model)" item-end>
</ion-toggle>
</ion-item>
有没有人有想法?
ion-toggle 需要一个布尔值,如果你将它绑定到一个布尔值,它就会起作用。 在您的 allRecs 模型属性中是字符串,因此初始值不会影响离子切换并且无法更改它。所以 x.model 应该是布尔值或为例如值添加一个新的布尔属性以将其设置为 ngModel:
constructor(){
this.allRecs = [
{
label: "label 1",
value: false
},
{
label: "label 2",
value: false
},
{
label: "label 3",
value: true
}
]
}
toggle(flag:boolean){
for(let i=0;i<this.allRecs.length;i++){
this.allRecs[i].value = flag;
}
}
在html中:
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.value" (ionChange)="toggle(x.value)" item-end>
</ion-toggle>
</ion-item>
我试着像上面的例子那样做,但需要像下面这样的一些改进。
constructor(){
this.allRecs = [
{
id: 1, //add this line
label: "label 1",
value: false
},
{
id: 2, //add this line
label: "label 2",
value: false
},
{
id: 3, //add this line
label: "label 3",
value: true
}
]
}
/*
* in this method added new parameter `id: number`
*/
toggle(id: number, flag:boolean) {
for(let i=0;i<this.allRecs.length;i++) {
//check if the current record has the same id
if (this.allRecs[i].id == id) {
this.allRecs[i].value = flag;
}
}
}
在html中:
<!-- added new parameter `x.id` when occurs `ionChange` event calling toggle method -->
<ion-item *ngFor="let x of allRecs">
<ion-label> {{x.label}} </ion-label>
<ion-toggle [(ngModel)]="x.value" (ionChange)="toggle(x.id, x.value)" item-end>
</ion-toggle>
</ion-item>