javascriptangulartypescriptangular-ng-if

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?


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>