Lokicor
Lokicor

Reputation: 137

*ngIf & Object.keys()

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')">

Edit: I appreciate the answers; I knew how to make it work (so for example using a function created in the .ts with the Object.keys() method and returning true or false), but my question was why can't this code be directly used *inside the ngIf, because everytime I write the word "Object" inside the *ngIf expression, WebStorm claims that error I mentioned.

Upvotes: 0

Views: 1021

Answers (2)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41447

use a getter variable

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

 <span *ngIf="checkValue">

Upvotes: 2

Matthieu Riegler
Matthieu Riegler

Reputation: 55450

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.

Upvotes: 4

Related Questions