2

I am new in Angular 4.x. I have a html table. Every row has a checkbox and a checkbox. I want to bind checkbox with button, so that when the checkbox is checked, the button is enabled, and when the checkbox in unchecked, the button is disabled: here is a sample of code, but it is not working :

  <tbody>
    <tr *ngFor="let item of mf.data; let i = index;">
      <td><input type="checkbox" value="" [checked]="item.checked"></td>

      <td>{{i}}</td>
      <td>{{item.name}}</td>
      <td>{{item.email}}</td>
      <td>{{item.age}}</td>
      <td>{{item.city | uppercase}}</td>
      <td><button type="button" class="btn btn-success" [disabled]="item.checked">start</button></td>
    </tr>
  </tbody>

Can you help me to make this work please ?

4 Answers 4

9

use [(ngModel)]. Because check will not enable the two-way binding. It just handles One-way changing

<td><input type="checkbox" value="" [(ngModel)]="item.checked"></td> 

make the button disable not equal to item check, like this [disabled]="!item.checked"

<td><button type="button" class="btn btn-success" [disabled]="!item.checked">start</button></td>
Sign up to request clarification or add additional context in comments.

Comments

3

Currently your checkbox only binds one way. To apply changes by clicking it, add the following to your input tag: (change)="item.checked = !item.checked"

Comments

0

You don't need checked directive. Just binding the model is enough.

  <tbody>
<tr *ngFor="let item of mf.data; let i = index;">
  <td><input type="checkbox" [(ngModel)]="item.checked"></td>

  <td>{{i}}</td>
  <td>{{item.name}}</td>
  <td>{{item.email}}</td>
  <td>{{item.age}}</td>
  <td>{{item.city | uppercase}}</td>
  <td><button type="button" class="btn btn-success" [disabled]="!item.checked">start</button></td>
<pre>{{item.checked}}</pre>
</tr>
  </tbody>

  <tbody>
    <tr *ngFor="let item of mf.data; let i = index;">
      <td><input type="checkbox" value="" [checked]="item.checked"></td>

      <td>{{i}}</td>
      <td>{{item.name}}</td>
      <td>{{item.email}}</td>
      <td>{{item.age}}</td>
      <td>{{item.city | uppercase}}</td>
      <td><button type="button" class="btn btn-success" [disabled]="item.checked">start</button></td>
    </tr>
  </tbody>

Comments

0

Use this:

    <td>
       <div class="togglebutton">
          <label>
            <input type="checkbox" [checked]="record.status" (change)="changeStatus(record.id,$event)">
             <span class="toggle"></span>

            </label>
       </div>
    </td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.