Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2, disable button if no checkbox selected

I have multiple checkboxes and a button that has to be enabled only if at least one checkbox is selected

<input type="checkbox">VALUE1
<input type="checkbox">VALUE2
<input type="checkbox">VALUE3
<input type="checkbox">VALUE4
<button>Proceed</button>

How is this achieved using Angular2.

P.S: found similar questions but not using Angular2.

like image 535
Khaled Avatar asked Dec 07 '15 18:12

Khaled


People also ask

How do you disable a button until a checkbox is checked in angular?

We will use the Angular JS directive named ng-disabled to disable the button by unchecking the box. Please refer to AngularJS ng-disabled Directive. The ng-disabled directive is used to enable or disable the HTML elements.


4 Answers

One way is to use ngModel on each checkbox, then control the button's disabled property via a method that examines each checkbox model state:

@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);
  }
}

Plunker

like image 177
Mark Rajcok Avatar answered Oct 16 '22 18:10

Mark Rajcok


Use the propertie [disable] as:

<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>
like image 22
Emir Marques Avatar answered Oct 16 '22 18:10

Emir Marques


You can perform any action by using $event in change event of checkbox.

Sample:

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;
        }
    }
like image 33
Tk1993 Avatar answered Oct 16 '22 18:10

Tk1993


I faced same issue in my project and i solved it.

sample:

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);
    }
}
like image 24
Neel Patel Avatar answered Oct 16 '22 18:10

Neel Patel