0

I have a list of lists and I want to hide/show internal list based on an attribute value

<md-list *ngFor="let el of data">
    <md-list-item *ngFor="let item of el.items" >
      <h4 mdSubhead]er>{{item.name}}</h4>
    </md-list-item>h4
  </md-list>

I have tried with *ngIf, but Angular4 permit only one template binding for an element.

How can I implement this beavior?

theShadow89
  • 1,287
  • 19
  • 41
  • what is the show / hide condition? You may use an `ng-container` or try with the `[hidden]` attribute. – briosheje Sep 01 '17 at 16:43
  • 1
    Possible duplicate of [\*ngIf and \*ngFor on same element causing error](https://stackoverflow.com/questions/34657821/ngif-and-ngfor-on-same-element-causing-error) – Z. Bagley Sep 01 '17 at 17:06

4 Answers4

3

You can just do by adding a extra div with a condition or ng-container which would be the best for angular

<md-list *ngFor="let el of data">
  <ng-container *ngIf="el.something">
    <md-list-item *ngFor="let item of el.items" >
      <h4 mdSubheader>{{item.name}}</h4>
    </md-list-item>
  </ng-container>
 </md-list>
Sajeetharan
  • 203,447
  • 57
  • 330
  • 376
2

Use ng-container (no need of extra element) and on the same use *ngIf to hide/show DOM conditionally.

<md-list *ngFor="let el of data">
  <ng-container *ngIf="el.show">
    <md-list-item *ngFor="let item of el.items" >
      <h4 mdSubhead]er>{{item.name}}</h4>
    </md-list-item>
  </ng-container>
</md-list>
Pankaj Parkar
  • 130,886
  • 22
  • 223
  • 289
1

I think best way might be to get data before, then remove unneeded items and iterate through remaining lists. But if really need in template, so you can use ng-template and long *ngFor version, so something like:

<ng-template ngFor let-el [ngForOf]="data" let-i="index">
 <md-list #el>
 <ng-template ngFor let-item [ngForOf]="el.items" let-i="index">
  <md-list-item #item>
    </md-list-item>h4
  </ng-template>
 </md-list>
</ng-template>
Julius Dzidzevičius
  • 10,357
  • 10
  • 33
  • 75
0

Use <ng-container> as mentioned here : <ng-container> to the rescue

<md-list *ngFor="let el of data">
    <ng-container *ngIf="your-condition-here">
        <md-list-item *ngFor="let item of el.items" >
            <h4 mdSubhead]er>{{item.name}}</h4>
        </md-list-item>
    </ng-container>
</md-list>
Vikhyath Maiya
  • 2,938
  • 1
  • 28
  • 57
Sasidhar Vanga
  • 3,364
  • 2
  • 25
  • 46