*ngIf and *ngFor on same element causing error

后端 未结 16 2094
自闭症患者
自闭症患者 2020-11-22 02:37

I\'m having a problem with trying to use Angular\'s *ngFor and *ngIf on the same element.

When trying to loop through the collection in th

相关标签:
16条回答
  • 2020-11-22 02:48

    Angular v2 doesn't support more than one structural directive on the same element.
    As a workaround use the <ng-container> element that allows you to use separate elements for each structural directive, but it is not stamped to the DOM.

    <ng-container *ngIf="show">
      <div *ngFor="let thing of stuff">
        {{log(thing)}}
        <span>{{thing.name}}</span>
      </div>
    </ng-container>
    

    <ng-template> (<template> before Angular v4) allows to do the same but with a different syntax which is confusing and no longer recommended

    <ng-template [ngIf]="show">
      <div *ngFor="let thing of stuff">
        {{log(thing)}}
        <span>{{thing.name}}</span>
      </div>
    </ng-template>
    
    0 讨论(0)
  • 2020-11-22 02:53

    As @Zyzle mentioned, and @Günter mentioned in a comment (https://github.com/angular/angular/issues/7315), this is not supported.

    With

    <ul *ngIf="show">
      <li *ngFor="let thing of stuff">
        {{log(thing)}}
        <span>{{thing.name}}</span>
      </li>
    </ul>
    

    there are no empty <li> elements when the list is empty. Even the <ul> element does not exist (as expected).

    When the list is populated, there are no redundant container elements.

    The github discussion (4792) that @Zyzle mentioned in his comment also presents another solution using <template> (below I'm using your original markup ‐ using <div>s):

    <template [ngIf]="show">
      <div *ngFor="let thing of stuff">
        {{log(thing)}}
        <span>{{thing.name}}</span>
      </div>
    </template>
    

    This solution also does not introduce any extra/redundant container elements.

    0 讨论(0)
  • 2020-11-22 02:57

    This will work but the element will still in the DOM.

    .hidden {
        display: none;
    }
    

    <div [class.hidden]="!show" *ngFor="let thing of stuff">
        {{log(thing)}}
        <span>{{thing.name}}</span>
    </div>
    
    0 讨论(0)
  • 2020-11-22 02:58
    <div *ngFor="let thing of show ? stuff : []">
      {{log(thing)}}
      <span>{{thing.name}}</span>
    </div>
    
    0 讨论(0)
  • 2020-11-22 02:58

    You can't use multiple structural directive on same element. Wrap your element in ng-template and use one structural directive there

    0 讨论(0)
  • 2020-11-22 02:58

    On other solution might be to put an empty array in your for loop in the case where you don't want to display it

    <div *ngFor="let thing of show ? stuff : []">
    

    Where "stuff" is an array of "thing" and "show" the boolean to display or not the content

    0 讨论(0)
提交回复
热议问题