Ranganath Prasad
Angular - Can we apply two structural directives *ngIf and *ngFor on a same element ?

Will the below code work?
<div *ngIf="products" *ngFor="let product of products"> {{product.name}} </div>

By Ranganath Prasad in Angular on Jan 30 2022
  • Pramila Shende
    Feb, 2022 16

    we cannot use two structural directives on same element.Structural directives like ngfor can do complex things with the host element and its childrens. When two directives placed on same element we cannot decide which one takes precedence i.e., which should execute first *ngIf or *ngFor? What if *ngFor executes before *ngIf, which is supposed to display or hide the elements.Use ngFor and ngIf on same element It’s very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.

  • 0


Most Popular Job Functions


MOST LIKED QUESTIONS