3

For example consider the code:

<div *ngIf="-------NOTEMPTY-------">
  Parent
  <div *ngFor = 'let child of offsprings'>
    <div name="c1" *ngIf="conditions[child.name]">
      Child0
    </div>
  </div>
</div>

I want Parent division's ngIf to be false if all its child divisions' ngIf are false.

Note : It is not possible to me to collect all the child conditions in the ts file.

2 Answers2

1

Try adding in the Parent *ngIf condition

!offsprings.every(child => !conditions[child.name]);
Nil Llisterri
  • 787
  • 7
  • 19
0

Used a function call as *ngIf = checkIfExists() on parent.

<div *ngIf="checkIfExists()">
  Parent with children existing
  <div *ngFor = 'let child of offsprings'>
    <div name="c1" *ngIf="conditions[child.name]">
      Child0 {{conditions[child.name]}}
    </div>
  </div>
</div>

Function For Parent ngIf:
Appraoch is to traverse every element of offsprings array,
and check if conditions[child.name] exists.

checkIfExists() {
  let ifDefined: boolean;
    this.offsprings.forEach(e=> {
      if (this.conditions[e.name]) {
        ifDefined = true;
      } 
    });
    return ifDefined;
}

StackBlitz Working Demo

Abhishek Kumar
  • 2,501
  • 10
  • 25