0

This code below does not work! How can i do ng-for inside a ng-for?

<ul *ng-for="#item of items">
    <li *ng-for="#task of item.tasks">{{task.title}}</li>
</ul>
nircraft
  • 8,242
  • 5
  • 30
  • 46

1 Answers1

3

One of the possible errors: you might forgot to specify NgFor or CORE_DIRECTIVES in directives property of the View decorator:

import {Component, View, NgFor} from 'angular2/angular2';

@Component()
@View({
  template: `...`,
  directives: [NgFor] // <- You might forgot to do this
})
export class SomeCompoennt { ... }

See this plunker also

UPD Problem with this your plunker

The problem is that p (paragraph element) cannot contain block elements. See this.

The P element represents a paragraph. It cannot contain block-level elements (including P itself).

So browser converts any nested ps:

<p>
  <p>
    <p>
    </p>
  </p>
</p>

into flat structure:

<p></p>
<p></p>
<p></p>

So code:

<p *ng-for="#person of peopleService.people">
  {{person.name}}
  <table>
      <tr *ng-for='#hobby of person.hobbies'>
          <td>{{hobby}</td>
      </tr>
  </table>
</p>

will be converted by browser into:

<p *ng-for="#person of peopleService.people">
  {{person.name}}
</p>
<table>
  <tr *ng-for='#hobby of person.hobbies'>
    <td>{{hobby}</td>
  </tr>
</table>

Of course person is undefined in this situation.

alexpods
  • 47,475
  • 10
  • 100
  • 94
  • 1
    This thing doesn't like `

    `s apparently. With `

    `s works pretty well http://plnkr.co/edit/QlW890DprENDdhOrcxeB?p=preview
    – Eric Martinez Sep 30 '15 at 19:13
  • 1
    Ooh, thanks to you, Eric, I've understood what is going on. Look at [this](http://www.w3.org/TR/html401/struct/text.html#h-9.3.1). **

    cannot contain block-level elements**

    – alexpods Sep 30 '15 at 19:44
  • 1
    Yes, I asked in gitter and I was told the same. [Here's a list](https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements) of `block-level elements`. This list includes `table` which was inside `

    ` in the OP's plnkr originally.

    – Eric Martinez Sep 30 '15 at 20:05
  • 2
    Yeah, but for html5 it's even more tricky. See [this SO answer](http://stackoverflow.com/questions/9852312/list-of-html5-elements-that-can-be-nested-inside-p-element#9852381) – alexpods Sep 30 '15 at 20:13
  • @alexpods as you already know, `ng-for` has become `ngFor` and `#` became `let`. So `

    `

    – Memet Olsen May 09 '16 at 08:26