0

I'm trying to generate a lists of checkboxes using ngfor. Everything works fine but if I check the 1st checkbox on the bottom card row it seems to think I checked the 1st box of the 1st card row.

Here's a small gif that demonstrates the issue. http://www.giphy.com/gifs/dQpr1jkRci0DaZoLMl

<div *ngFor="let task of taskdata" class="card" >

    <div *ngIf="task.task1" class="custom-control custom-checkbox">
          <input type="checkbox" class="custom-control-input" id="customCheck1" 
                (change)="getdataonID($event.target.checked)" [checked]="task.isDone1">
          <label class="custom-control-label" for="customCheck1">{{task.task1}}</label>
    </div>
0

3 Answers 3

3

All your checkboxes have the same ID. And clicking on any of the labels is supposed to select the checkbox identified by customCheck1. But there are many of them. So that can't work.

There might be other problems, but you didn't post all the relevant code.

Sign up to request clarification or add additional context in comments.

3 Comments

This was it. Having check box id was the problem. After I erased that it fixed the issue thanks! Sorry about not posting the whole code I will do it next time :)
@RandikaPeiris you shouldn't remove the ID. That will make it impossible to click the label to check or uncheck the checkbox. Instead, you should use unique IDs.
Allright, I haven't heard of that but will do some research. Anyway although I removed the ID, I'm able to check and uncheck the box and my functions work as well but as you said clicking the label is not possible anymore
2

use the [(ngModel)] with the checked attribute

 <input type="checkbox" class="custom-control-input" id="customCheck1" 
 (change)="getdataonID($event.target.checked)"  [(ngModel)]="task.isDone1">

2 Comments

I'm using [checked] to get the previous state of the checkbox as stored in the DB. If I use ngmodel will will it affect that?
Nope it wont affect
0

Check this exmaple, I hope it will work for you. Instead of the event I am calling the method and passing element data as param and performing calculations in the component.

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.