【发布时间】:2016-10-07 04:41:02
【问题描述】:
我有一个复选框列表。在此我想禁用其他复选框当我检查了列表中的任何项目时。现在,如果我取消选中该项目,那么现在需要启用所有复选框。
这是笨蛋 - http://plnkr.co/edit/5FykLiZBQtQb2b31D9kE?p=preview
取消选中任何复选框后,所有其余复选框仍处于禁用状态。我该怎么做?
这是 app.ts 文件 -
import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
template: `
<h2>{{title}}</h2>
<label *ngFor="let cb of checkboxes">
{{cb.label}}<input [disabled]="cb.status" type="checkbox"
[(ngModel)]="cb.state" (click)="buttonState(cb.id)">
</label><br />
{{debug}}
`
})
class App {
title = "Angular 2 - enable button based on checkboxes";
checkboxes = [{label: 'one',id: '0', status: ''},{label: 'two', id: '1', status:''},{label: 'three', id: '2', status: ''}];
constructor() {
//this.buttonState();
console.log("constructor called"); }
buttonState(id) {
console.log( id + "Button State Called");
//return this.checkboxes[0].status;
if(this.checkboxes[id].state == true){
this.checkboxes[id].status = true;
this.checkboxes[(id+1)%3].status = false;
this.checkboxes[(id+2)%3].status = false;
console.log("True Block");
}
else (this.checkboxes[id].state == false) {
this.checkboxes[id].status = false;
this.checkboxes[(id+1)%3].status = true;
this.checkboxes[(id+2)%3].status = true;
console.log("False Block");
}
}
get debug() {
return JSON.stringify(this.checkboxes);
}
}
bootstrap(App, [])
.catch(err => console.error(err));
【问题讨论】:
标签: checkbox angular event-handling