Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular *ngIf check if object only contains null values

I can check if the object itself is null with (myObj | keyvalue)?.length. (https://stackoverflow.com/a/56532650/13797956)

With JS I can check if the object contains only null values with Object.values(myObj).some(x => x !== null).
But when I try to use this in the template I get Parser Error: Bindings cannot contain assignments.

Is there a way to check if myObj only contains null values inside the template or should I use a function that does the work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For this you should create a EveryPipe for that.

@Pipe({ name: 'every' })
export class ContainsPipe implements PipeTransform {
   transform(input: any, value: any): boolean {
      return Object.values(input).every(x => x == value)
   }
}

used like following

<div *ngIf="(myObj | every:null)"> 
   ...
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading