如何选择angular 2中的所有复选框?
如何选择angular 2中的所有复选框?,angular,Angular,全选 {{user}} 这是angular 2中的一个解决方法,您可以参考下面的代码 HTML {{n.name} 组成部分 import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComp
全选
{{user}}
这是angular 2中的一个解决方法,您可以参考下面的代码
HTML
-
-
{{n.name}
组成部分
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title: String;
names: any;
selectedAll: any;
constructor() {
this.title = "Select all/Deselect all checkbox - Angular 2";
this.names = [
{ name: 'Prashobh', selected: false },
{ name: 'Abraham', selected: false },
{ name: 'Anil', selected: false },
{ name: 'Sam', selected: false },
{ name: 'Natasha', selected: false },
{ name: 'Marry', selected: false },
{ name: 'Zian', selected: false },
{ name: 'karan', selected: false },
]
}
selectAll() {
for (var i = 0; i < this.names.length; i++) {
this.names[i].selected = this.selectedAll;
}
}
checkIfAllSelected() {
this.selectedAll = this.names.every(function(item:any) {
return item.selected == true;
})
}
}
ToggleChb(state:boolean){
this.Users.forEach(user=> user.selected = state)
}
从'@angular/core'导入{Component};
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
标题:字符串;
姓名:任何;
selectedAll:任意;
构造函数(){
this.title=“全选/取消全选复选框-角度2”;
此文件名=[
{name:'Prashobh',selected:false},
{name:'Abraham',选中:false},
{name:'Anil',selected:false},
{name:'Sam',selected:false},
{name:'Natasha',selected:false},
{name:'Marry',选中:false},
{name:'Zian',selected:false},
{name:'karan',selected:false},
]
}
selectAll(){
for(var i=0;i
在html中
<div *ngFor="let user of Users">
<input type="checkbox" [(ngModel)]="user.selected" (change)="ToggleChb();">
</div>
这里有些紧凑的解决方案工作得很好,感谢当数据来自API请求时,这种方法将不起作用。
ToggleChb(state:boolean){
this.Users.forEach(user=> user.selected = state)
}