Ionic2 离子2/3:如何以编程方式取消选中复选框

Ionic2 离子2/3:如何以编程方式取消选中复选框,ionic2,Ionic2,我有一种情况,我必须取消选中复选框,只要点击一个按钮 <button class="button-m" (click)="navigateTo()" ion-button color="secondary"><ion-icon name="next">&nbsp;&nbsp;</ion-icon> Next </button> 这是我的复选框代码 <ion-checkbox [checked]="!isChecked" [d

我有一种情况,我必须取消选中复选框,只要点击一个按钮

<button class="button-m" (click)="navigateTo()" ion-button color="secondary"><ion-icon name="next">&nbsp;&nbsp;</ion-icon> Next </button>
这是我的复选框代码

<ion-checkbox [checked]="!isChecked" [disabled]="!isdisabled" (ionChange)="saveProfile($event)" [(ngModel)]="default"></ion-checkbox>

您的代码中有一个错误
this.isChecked==true未将
isChecked
变量设置为true。它只是做一个比较来检查
isChecked
是否为真。 您应该使用
=
而不是
=

将代码更改为如下所示:

  navigateTo()
  {
    console.log("Next Clicked")
    this.isChecked = true;
  }

您的代码中有一个错误
this.isChecked==true未将
isChecked
变量设置为true。它只是做一个比较来检查
isChecked
是否为真。 您应该使用
=
而不是
=

将代码更改为如下所示:

  navigateTo()
  {
    console.log("Next Clicked")
    this.isChecked = true;
  }

不要将
checked
属性用作绑定。您的输入与
ngModel
绑定,因此您需要做的是更改该字段的值

修复了double之后,我感到很惊讶,因为模板正在查找
默认值
,并根据其值设置checked属性。创建一个变量并绑定到它,然后更改它。我重写了你的组件

然后在组件中:

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);
  }
}

不要将
checked
属性用作绑定。您的输入与
ngModel
绑定,因此您需要做的是更改该字段的值

修复了double之后,我感到很惊讶,因为模板正在查找
默认值
,并根据其值设置checked属性。创建一个变量并绑定到它,然后更改它。我重写了你的组件

然后在组件中:

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);
  }
}

为什么不取消选中
navigateTo()
?显示您的.ts文件这就是我遇到问题的地方,它没有被取消选中为什么不在
navigateTo()
上取消选中它?显示你的.ts文件这就是我遇到的问题,它并没有被取消选中,我只是不相信我忽略了这么简单的语法问题。我现在真的应该休息一下:)。谢谢你指出!如果这个问题没有给这个令人敬畏的社区增加任何价值,请删除这个问题。你可以调整“checked”属性,但你应该真正关注ngModel的价值,这样你就可以在没有模板的情况下编写测试、更改等。我真不敢相信我忽略了这么简单的语法问题。我现在真的应该休息一下:)。谢谢你指出!如果没有为awesome社区添加任何值,请删除此问题。您可以调整“checked”属性,但您应该真正关注ngModel的值,这样您就可以在没有模板的情况下编写测试、更改等