import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
title = "Angular 2 - enable button based on checkboxes";
checkboxes = [{label: 'one',selected:false},
{label: 'two',selected:false}
];
selectedFileList:any[]=[];
checkClicked(event: Event, i) {
if (event.target['checked']) {
this.selectedFileList.push(i);
console.log("selected checkBox is: "+this.selectedFileList);
}
else {
let index = this.selectedFileList.indexOf(i);
if (index !== -1) this.selectedFileList.splice(index, 1);
console.log("inside else")
this.selectedFileList=[];
}
}
constructor() { console.clear(); }
buttonState() {
// console.log('buttonState() called');
return !this.checkboxes.some(cb => cb.selected) && (this.selectedFileList.length=1);
}
get debug() {
return JSON.stringify(this.checkboxes);
}
}
我正在检查selectedFileList的长度,以检查是否选择了超过1个cb,但无法这样做,请帮忙。
仅当选择1 cb时才启用按钮,而选择0或大于1时则禁用
stackBlitz: https://stackblitz.com/edit/angular-cjccyp?file=src%2Fapp%2Fapp.component.ts