离子2多项选择,选择所有复选框

时间:2017-12-01 06:19:47

标签: angular typescript ionic-framework ionic2 ionic3

我希望通过离子选择提供选择选项。我在第一个位置手动添加了选择所有选项,当我选择所有视图未更新时。如何更新视图?

enter image description here html的

<ion-select multiple="true">
      <ion-option (ionSelect)="allClicked()">select all</ion-option>
      <ion-option *ngFor="let item of students ; let i = index" [selected]="item.checked" [value]="item">{{item.studentName}}</ion-option>
</ion-select>

.TS

       allClicked(){
        if(this.isAll){
           console.log(this.isAll)
            this.isAll = false;
            for (let temp of this.students) {
             temp.checked = false;
               }
        }else{
        this.isAll = true;

        for (let temp of this.students) {
           temp.checked = true;
           }
          }
        console.log("all select event ");
        }

2 个答案:

答案 0 :(得分:0)

就我而言,我有一个非常相似的问题。注入ChangeDetectorRef并调用cdRef.detectChanges()

因此代码必须类似于:

import {Component, OnInit, ChangeDetectorRef} from 'angular2/core';

export class RecentDetectionComponent implements OnInit, OnDestroy {

constructor(private cdRef: ChangeDetectorRef // <== added ) {

}



 allClicked(){
   if(this.isAll){
   console.log(this.isAll)
   this.isAll = false;
   for (let temp of this.students) 
   {
   temp.checked = false;
   }
   }else{
   this.isAll = true;
   for (let temp of this.students) {
   temp.checked = true;
   }
   }
   console.log("all select event ");
   this.cdRef.detectChanges(); // <== added
   }
}

答案 1 :(得分:-1)

一个解决方案是将所有学生都放入模型中进行离子选择

- (UIStatusBarStyle) preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}

<强> TS。

<ion-select multiple="true" [(ngModel)]="selectStudents">
      <ion-option (ionSelect)="allClicked()">select all</ion-option>
      <ion-option *ngFor="let item of students ; let i = index" [selected]="item.checked" [value]="item">{{item.studentName}}</ion-option>
</ion-select>

我假设学生是一群学生。