0

I'm trying to repeat a nested object and I have a checkbox with a value of true or false.

Here's my code:

<accordion-group
    *ngFor='let brand of products'>
    <div *ngFor='let product of brand.products'>
        <div>
           <input type="checkbox" id="{{product.name}}" ngControl="{{product}}" ([ngModel])="product.checked">
             <label
                attr.for="{{product.name}}"
                id="{{product.description}}"
                class="{{product.description}}"
                (click)="onCheck(model)">
             </label>
          </div>
        </p>
      </div>
    </accordion-group>

Plunker: http://plnkr.co/edit/IItxHiXkpocUjLG6NhMy?p=preview -- see that the checkbox doesn't check when the quantity value is updated. I can't get ngModel to work correctly.

1
  • 3
    Put a name attribute to the checkboxes. If they are supposed to belong to the same group they should have same names. Also you can make this question more clear if you add a fiddle with this code so we can better understand the problem, seeing the code running and failing. Commented Aug 29, 2016 at 22:39

3 Answers 3

2
  • ngControl should be fromControlName according to angular2 RC.5

  • ([ngModel]) should be [(ngModel)] or used as different one like [ngModel] and (ngModel) .

  • better to use property Binding using [ ] while binding id ,name etc dynamicaly as in your case use

use [id]="product?.name" instead of id="{{product.name}}"

use [attr.for]="product?.name" instead of attr.for="{{product.name}}"

seems Rest is fine everything, if still having same issue please reproduce your problem on plunker and post it.

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

2 Comments

See plunker linked to in edited question. I need the checkbox to check when the quantity updates but it doesn't work. The ngModel isn't binding the way it should
you were right. I got my plunker to work. Now I just need to figure out why it doesn't work in my app. Thanks!
1

This is invalid

([ngModel])

and should be

[(ngModel)]

It's called "banana in a box", not "box in a banana" ;-)

2 Comments

But you see it's helpful :D
yeah no doubt :p
0

[(ngModel)]="product.checked";

100% working in angular 2

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.