Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Checking the value of a variable on and *ngIf statement

I am using angular 5 and I'm trying to check the value of a variable in the html template of the component.

So it looks something like this:

<div *ngIf="item='somevalue'">

I'm getting this error:

ht Error: Template parse errors:

Parser Error: Bindings cannot contain assignments at column 17 in...

Can't this be done in angular?


2 Answers

*ngIf work like this *ngIf="expression" where the expression is replaces with the simple Javascript statement which returns boolean. But you use one = and it means that you assign the someValue to the item property and if the value is not falsy it will return you true.

In your case you need to write *ngIf="item === 'somevalue'".

like image 122
Suren Srapyan Avatar answered Sep 02 '25 13:09

Suren Srapyan


You are making assignment instead of comparison

<div *ngIf="item==='somevalue'">
like image 33
edkeveked Avatar answered Sep 02 '25 14:09

edkeveked