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

问题描述 投票:0回答:3
ionic2
3个回答
1
投票

您的代码有错误。

this.isChecked == true;
不会将
isChecked
变量设置为 true。它只是进行比较以检查
isChecked
是否为真。 您应该使用
=
而不是
==

将您的代码更改为以下内容:

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

1
投票

不要使用

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

0
投票

您可以获取 IonCheckbox 实例的引用并将其设置为 check = false;

使用@ViewChild():

//.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;
}

(同样,可能需要检测变化。)

© www.soinside.com 2019 - 2024. All rights reserved.