您的代码有错误。
this.isChecked == true;
不会将 isChecked
变量设置为 true。它只是进行比较以检查 isChecked
是否为真。
您应该使用 =
而不是 ==
。
将您的代码更改为以下内容:
navigateTo()
{
console.log("Next Clicked")
this.isChecked = true;
}
不要使用
checked
属性作为绑定。您的输入与 ngModel
绑定,因此您需要做的是更改该字段的值。
修复 double equals 后您的代码有效,我感到很惊讶,因为模板正在寻找
default
并根据其值设置选中的属性。创建一个变量并绑定到它,然后更改它。我稍微重写了你的组件
然后在你的组件中:
import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ToastController } from 'ionic-angular';
@Component({
selector: 'choose-profile',
templateUrl: 'choose-profile.html',
})
export class ChooseProfilePage {
public shouldSaveProfile = false;
profileValue : string;
isdisabled : boolean;
constructor(public navCtrl: NavController, public navParams: NavParams, private toastCtrl: ToastController) { }
getSelectedProfile(val){
this.profileValue = val;
this.isdisabled = this.profileValue ? true : false;
}
saveProfile(val){
if(this.shouldSaveProfile)
this.presentToast( this.profileValue+"set as default profile")
else this.presentToast("No default profile")
}
presentToast(val){
let toast = this.toastCtrl.create({
message: val,
duration: 3000,
position: 'top'
});
toast.present();
}
navigateTo()
{
this.shouldSaveProfile = true;
console.log("Next Clicked, should save value:", this.shouldSaveProfile);
}
}
您可以获取 IonCheckbox 实例的引用并将其设置为 check = false;
//.html
<ion-checkbox #checkbox></ion-checkbox>
//.ts
@ViewChild('checkbox') el: IonCheckbox;
this.el.checked = false;
(之后您可能需要激活更改检测 - 在构造函数中注入 ChangeDetectorRef,然后运行 this.cdr.detectChanges() )
//.html
<ion-checkbox #checkbox></ion-checkbox>
<ion-button (click)="removeCheck(checkbox)">click</ion-button>
//.ts
removeCheck(checkbox: IonCheckbox) {
checkbox.checked = false;
}
(同样,可能需要检测变化。)