Stealth Rabbi Stealth Rabbi - 3 months ago 9
AngularJS Question

optionall adding text at the end of an angular expression

I have an expression that's pull data from a controller, the number of pages of a book.

If the data exists, I want to print "X pages", where X is the value. Otherwose, I want the HTML paragraph to be blank.

But, I can't get the syntax correct. Below, it always prints out ' pages', even if blank. How do I set up my expression?

<p>{{reviewFormCtrl.book.numPages + ' pages' || "" }}</p>


I tried doing ternary operator in the expression, but didn't have lukc

<p>{{(reviewFormCtrl.book.numPages > 0)?{{reviewFormCtrl.book.numPages + ' pages' :"" }}</p>

Answer

Try

<p ng-if="reviewFormCtrl.book.numPages">{{ reviewFormCtrl.book.numPages }} pages</p>