Angular2,如果没有选中复选框则禁用按钮
Angular2, disable button if no checkbox selected
我有多个复选框和一个只有在至少选中一个复选框时才必须启用的按钮
<input type="checkbox">VALUE1
<input type="checkbox">VALUE2
<input type="checkbox">VALUE3
<input type="checkbox">VALUE4
<button>Proceed</button>
这是如何使用 Angular2 实现的。
P.S: 发现类似的问题但没有使用Angular2.
使用属性[禁用]广告:
<input type="checkbox" [(ng-model)]="disableButton1"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton2"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton3"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton4"> VALUE1
<button type="button" [disabled]="disableButton || disableButton2">Submit</button>
一种方法是在每个复选框上使用 ngModel
,然后通过检查每个复选框模型状态的方法控制按钮的 disabled
属性:
@Component({
template: `
<label *ngFor="let cb of checkboxes">
<input type="checkbox" [(ngModel)]="cb.state">{{cb.label}}
</label>
<p><button [disabled]="buttonState()">button</button>
`
})
class App {
checkboxes = [{label: 'one'},{label: 'two'}];
constructor() {}
buttonState() {
return !this.checkboxes.some(_ => _.state);
}
}
您可以在复选框的更改事件中使用 $event 来执行任何操作。
样本:
HTML
<input type="checkbox" (change)="changeEvent($event)" />
<button [disabled]="toogleBool">button</button>
TS
toggleBool: boolean=true;
changeEvent(event) {
if (event.target.checked) {
this.toggleBool= false;
}
else {
this.toggleBool= true;
}
}
我在我的项目中遇到了同样的问题,我解决了它。
样本:
HTML
<table class="table">
<thead>
<tr>
<th>Select</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of items">
<td><input type="checkbox" [(ngModel)]="item.chosen"></td>
<td>{{item.name}}</td>
</tr>
</tbody>
</table>
<button [disabled]="noneSelcted()">OK</button>
TS
import {Componnet} from '@angular/core'
@Componnet({
selector: 'my-test',
templateUrl: 'app/home/test.component.html'
})
export class TestComponent{
items = [
{ name: 'user1', chosen: false},
{ name: 'user2', chosen: false},
{ name: 'user3', chosen: false},
];
noneSelected(){
return !this.items.some(item => item.chosen);
}
}
我有多个复选框和一个只有在至少选中一个复选框时才必须启用的按钮
<input type="checkbox">VALUE1
<input type="checkbox">VALUE2
<input type="checkbox">VALUE3
<input type="checkbox">VALUE4
<button>Proceed</button>
这是如何使用 Angular2 实现的。
P.S: 发现类似的问题但没有使用Angular2.
使用属性[禁用]广告:
<input type="checkbox" [(ng-model)]="disableButton1"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton2"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton3"> VALUE1
<input type="checkbox" [(ng-model)]="disableButton4"> VALUE1
<button type="button" [disabled]="disableButton || disableButton2">Submit</button>
一种方法是在每个复选框上使用 ngModel
,然后通过检查每个复选框模型状态的方法控制按钮的 disabled
属性:
@Component({
template: `
<label *ngFor="let cb of checkboxes">
<input type="checkbox" [(ngModel)]="cb.state">{{cb.label}}
</label>
<p><button [disabled]="buttonState()">button</button>
`
})
class App {
checkboxes = [{label: 'one'},{label: 'two'}];
constructor() {}
buttonState() {
return !this.checkboxes.some(_ => _.state);
}
}
您可以在复选框的更改事件中使用 $event 来执行任何操作。
样本:
HTML
<input type="checkbox" (change)="changeEvent($event)" />
<button [disabled]="toogleBool">button</button>
TS
toggleBool: boolean=true;
changeEvent(event) {
if (event.target.checked) {
this.toggleBool= false;
}
else {
this.toggleBool= true;
}
}
我在我的项目中遇到了同样的问题,我解决了它。
样本:
HTML
<table class="table">
<thead>
<tr>
<th>Select</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of items">
<td><input type="checkbox" [(ngModel)]="item.chosen"></td>
<td>{{item.name}}</td>
</tr>
</tbody>
</table>
<button [disabled]="noneSelcted()">OK</button>
TS
import {Componnet} from '@angular/core'
@Componnet({
selector: 'my-test',
templateUrl: 'app/home/test.component.html'
})
export class TestComponent{
items = [
{ name: 'user1', chosen: false},
{ name: 'user2', chosen: false},
{ name: 'user3', chosen: false},
];
noneSelected(){
return !this.items.some(item => item.chosen);
}
}