Angular ionic 2选中“全选”复选框进行多选
我希望提供带有离子选择功能的选择选项。我在第一个位置手动添加了“全选”选项,此时“全选”视图未更新。如何更新视图 .htmlAngular ionic 2选中“全选”复选框进行多选,angular,typescript,ionic-framework,ionic2,ionic3,Angular,Typescript,Ionic Framework,Ionic2,Ionic3,我希望提供带有离子选择功能的选择选项。我在第一个位置手动添加了“全选”选项,此时“全选”视图未更新。如何更新视图 .html <ion-select multiple="true"> <ion-option (ionSelect)="allClicked()">select all</ion-option> <ion-option *ngFor="let item of students ; let i = index" [se
<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>
一种解决方案是将所有学生放入模型中进行离子选择
<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>
我假设学生是一组学生。在我的例子中,我有一个非常类似的问题。注入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
}
}
从'angular2/core'导入{Component,OnInit,ChangeDetectorRef};
导出类RecentDetectionComponent实现OnInit、OnDestroy{
构造函数(私有cdRef:ChangeDetectorRef//控制台日志打印是否正确?@SurajRao是的,但模型未更新您能否解释更多,因为没有问题您需要重新表述问题..您的意思是切换选择所有不更新其他选项?@SurajRao问题更新检查
selectStudents:[];
function allClicked(){
selectStudents=[];
students.filter((data)=>{
this.selectStudents.push(data.studentName);
});
}
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
}
}