Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 [innerHTML] if else validation

I want to display different text in a button, depending on some JSON data.

Example (of course not working):

<button [innerHTML]='"Male" if user.gender==1 else "Female"'></button>

I know this functionality already exisits in e.g [ngClass]:

<button [ngClass]='{"btn-success": data.complete, "btn-primary": !data.complete}'>Some text</button>

Using the [ngClass] approach is displayed as [object:Object] and therefore not working.

Any solution?

like image 372
Vingtoft Avatar asked Dec 06 '22 16:12

Vingtoft


1 Answers

use ternary expression

<button [innerHTML]='user.gender == 1 ? "Male" : "Female"'></button>

anyway you don't need innerHtml for this. I'd do it like:

<button>{{ user.gender == 1 ? "Male" : "Female" }}</button>
like image 83
Yaroslav Grishajev Avatar answered Dec 09 '22 14:12

Yaroslav Grishajev