Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngIf One-line if statements | Angular 6

i want to change the name displayed on the iteration using ngIf, i am trying to do something like this:

*ngIf="detail.name=='dieselG_d'? detail.name='Diesel Green':Diesel Red

and the console recorded the following error:

Uncaught Error: Template parse errors:
like image 505
Ali Al Amine Avatar asked Dec 17 '18 18:12

Ali Al Amine


People also ask

Can we use two conditions in ngIf?

We can use multiple conditions in *ngIf with logical operator AND (&&) to decide the trustworthy of *ngIf expression. If all conditions are true, then element will be added to the DOM.

Why * is used in ngIf?

The asterisk is "syntactic sugar". It simplifies ngIf and ngFor for both the writer and the reader. Under the hood, Angular replaces the asterisk version with a more verbose form.

How do you check for equal conditions in ngIf?

Use double equal to == operator to check equality or better use === to check strict equality. Save this answer.


1 Answers

I think you're using the wrong directive here- *ngIf is moreso to check if we should display the content at all, not to make an assignment and change a a variable. It requires a statement that evaluates a true or false, and cannot do assignments.

Something like the following might work better for you, if your goal is to display a certain name, rather than to hide content with certain values.

<div> {{ detail.name === 'dieselG_d' ? 'Diesel Green' : 'Diesel Red' }} </div>
like image 92
Amethystx87 Avatar answered Nov 15 '22 05:11

Amethystx87