2

I'm having a hard time trying to loop over an array of nested objects using ng7

this is the data I have:

data = { 
    'title1': [
    {
      active: true, 
      id: 1 
    },
  {
      active: true, 
      id: 2 
    },
  {
      active: true, 
      id: 3 
    },
  {
      active: true, 
      id: 4 
    }],
  'title2': [
   {
      active: true, 
      id: 1 
    },
  {
      active: true, 
      id: 2 
    },
  {
      active: true, 
      id: 3 
    },
  {
      active: true, 
      id: 4 
    }]              
}

I need to print the titles e.g. 'title1' and the rest of the data should be shown nested, issue is, whenever I go for this approach, everything looks ok:

 <ul>
    <li *ngFor="let item of data| keyvalue">
        <p>{{ item.key }}</p> 
        <p *ngFor="let children of item.value | keyvalue" >
            {{ children.value.id}}
        </p>
    </li>
  </ul>

But whenever I switch my layout to an input checkbox like this:

       <ul>
        <li *ngFor="let item of data| keyvalue">
            <p>{{ item.key }}</p> 
            <label>
                <input type="checkbox" name="events" *ngFor="let children of item.value | keyvalue" />
                event item {{ children.value.id}}
            </label>
        </li>
      </ul>

I get the following error on the browser's console, and nothing renders:

ERROR TypeError: Cannot read property 'value' of undefined at Object.eval [as updateRenderer] on

{{ item.key }}

Any idea? I'm sure I'm missing something dumb,

7
  • Sorry, I made a typo while writing down the data, there is it edited Commented Dec 4, 2018 at 2:40
  • It wasn't an array originally, but an object changed the first [ ] for curly braces Commented Dec 4, 2018 at 2:42
  • Possible duplicate of Angular 2 set and bind checkboxes with a ngFor Commented Dec 4, 2018 at 2:43
  • Yes, I saw the change you made to the data object, but I don't see any difference between your two code snippets at the bottom. They look identical to me. Commented Dec 4, 2018 at 2:46
  • 2
    put your ng-for in the label Commented Dec 4, 2018 at 3:02

1 Answer 1

3

Your children reference is not in scope when you reference it because the event item {{children.value.id}} string is not contained in the <input> element.

Define the loop on your <label> instead of your <input> element:

<ul>
  <li *ngFor="let item of data | keyvalue">
      <p>{{ item.key }}</p> 
      <label *ngFor="let children of item.value | keyvalue">
          <input type="checkbox" name="events" />
          event item {{children.value.id}}
      </label>
  </li>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

That was it, thanks a lot sir. Also thanks for throwing in the explanation.

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.