Ben
Ben

Reputation: 16651

AngularJS string with or in output

The following will show an integer if set, and "-" if not set:

{{ movie.agemin || "-" }}

Now I would like to extend this to show the integer followed by " years" if set, or "-" if not set.

{{ movie.agemin + " years" || "-" }}

However, this now stops executing the or case and always adds " years" at the end, no matter the value.

How does this have to be changed to work correctly?

Upvotes: 1

Views: 38

Answers (2)

Nitish Kumar
Nitish Kumar

Reputation: 4870

Try this

<div ng-show="movie.agemin">{{ movie.agemin + " years"}}</div>
<div ng-hide="movie.agemin">-</div>

instead of div, you can use ant html tag

Upvotes: 0

AlwaysALearner
AlwaysALearner

Reputation: 43947

{{ movie.agemin && (movie.agemin + " years") || "-" }}

Upvotes: 4

Related Questions