【问题标题】:enable only one checked box in ionic4 app在 ionic 4 应用程序中仅启用一个复选框
【发布时间】:2019-09-22 17:41:58
【问题描述】:

我试图在 ionic 4 应用程序中仅启用一个复选框,我已经开始了一些代码,但现在我被卡住了,我想我必须使用 ngModel.. 我想在检查时将reponse.checked 更改为正确,然后将所有其他检查的数组设为 false...

html

  <ion-card>
    <ion-card-title>
      Question 1
    </ion-card-title>
      <ion-card-subtitle>
        Test
      </ion-card-subtitle>
        <ion-list>
              <ion-item *ngFor='let rep of reponse'>
                <ion-label>{{rep.name}}</ion-label>
                  <ion-checkbox 
                  [(ngModel)]="isChecked"
                  color="primary" 
                  slot="start"></ion-checkbox>
              </ion-item>
          </ion-list>
  </ion-card>

.ts

export class SurveyPage implements OnInit {

reponse = [
  {
    name: 'test',
    checked: false
  },
  {
    name: 'test2',
    checked: false
  },
];

  constructor() { }

  ngOnInit() {

  }

isChecked( {

});

}

【问题讨论】:

  • 这是单选按钮的默认行为,那你为什么要使用复选框呢?

标签: angular ionic4


【解决方案1】:

你可以使用ngModelChange重置其他选中的项目

<ion-item *ngFor='let rep of reponse;let i = index'>
         <ion-label>{{rep.name}}</ion-label>
           <ion-checkbox 
                  [(ngModel)]="rep.checked"
                  color="primary" 
                  (ngModelChange)="updatedCheckList(i)"
                  slot="start">
           </ion-checkbox>
</ion-item>

更新的CheckList方法

updatedCheckList(itemIndex:number) {
   this.reponse.forEach((rep,index) =>{
     if (itemIndex !== index) {
       rep.checked = false
     }
    })
}

demo ??

【讨论】:

  • @Yann92 很乐意帮助您为什么不使用收音机而不是复选框?
  • 我会的,我不知道它存在。不过好东西,我已经了解了ngModelChange的使用
猜你喜欢
  • 2014-11-30
  • 2018-02-07
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
相关资源
最近更新 更多