我希望通过离子选择提供选择选项。我在第一个位置手动添加了选择所有选项,当我选择所有视图未更新时。如何更新视图?
<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 ");
}
答案 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>
我假设学生是一群学生。