Home > Mobile >  *ngIf & Object.keys()
*ngIf & Object.keys()

Time:10-19

Why I can't use Object.keys() method inside an *ngIf directive of Angular? WebStorm arises Unresolved variable or type Object error, highlighting the "Object" word.

Example:

<span *ngIf="Object.keys(item).includes('author')">

CodePudding user response:

Like for enum, you need to make the reference public in your component to make it accessible in the angular template :

class MyComponent {
    public Object = Object
}

Anyway for your particular problem I'd created a custom pipe instead of calling this method in the template directly.

CodePudding user response:

use a getter variable

get checkValue(){
    return Object.keys(item).includes('author') 
}

 <span *ngIf="checkValue">
  • Related