0

I would like to use check boxes to select a parent object and then only show check boxes for a a property (array) on that object. For example i have an object of products and each product has a number of items. I want to show the check list of all items in the product but only if the product itself has been checked.

I tried the following code by it either shows or hides the items based on the initial value - i want to be able to toggle the check box on and off and hide or display the items.

<div *ngFor="let product of products; let i = index" #ref>
    <md-checkbox type="checkbox"> {{product.productTitle}}</md-checkbox>
    <div *ngIf="ref.checked">
       <div *ngFor="let item of product.items; let i = index">
          <md-checkbox type="checkbox">{{item.code}}</md-checkbox> - {{item.name}}
       </div>
    </div>
 </div>
6
  • Are your products defined within a FormArray? Commented Jul 28, 2017 at 17:27
  • Not currently, my plan is to take the selected items and display them in a form. I can put the products into a form if that would help? Commented Jul 28, 2017 at 17:32
  • Do you mind posting the component code, or at least the formGroup declaration? Commented Jul 28, 2017 at 17:34
  • Maybe I misunderstood your previous comment. Your #ref is on your div and you are checking if it is checked. The div will never return true for checked correct? You need to reference your <md-checkbox> and check for checked there. Commented Jul 28, 2017 at 17:36
  • Of course - how silly of me. That actually works exactly as i was expecting it to. Late at night :-) thanks for the assistance Commented Jul 28, 2017 at 17:39

1 Answer 1

4

Your #ref is on your div and you are checking if it is checked. The div will never return true for checked correct? You need to reference your and check for checked there.

For example (Depending on how your md-checkbox component is configured):

<div *ngFor="let product of products; let i = index">
    <md-checkbox type="checkbox" #ref> {{product.productTitle}}</md-checkbox>
    <div *ngIf="ref.checked">
       <div *ngFor="let item of product.items; let i = index">
          <md-checkbox type="checkbox">{{item.code}}</md-checkbox> - {{item.name}}
       </div>
    </div>
 </div>
Sign up to request clarification or add additional context in comments.

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.