0

I have a method to that returns an integer value, {{Cart.totalItems()}}. This works fine. I wanted to know if it is possible to have this expression print a string based on the result of this method call. For example:

print "X" if the value returned is greater than 0, or "Y" otherwise.

Is this possible in the expression itself (for example by using a filter) or do I need to roll an Angular function to do this? I've used similar techniques done in the expression specified in an ng-showdirective, for example:

{true:'false',false:'true'}[ngTransaction.$state.shippingNameSameAsCustomerName]

However, I cannot seem to nail the syntax for this in view. Is it possible to render a string conditionally based on the output of a function call in an expression?

1 Answer 1

1

You should be able to use the JavaScript ternary operator inside an interpolated template:

{{ Cart.totalItems() > 123 ? 'Happy' : 'Sad' }}

The above code will print Happy if the value returned by Cart.totalItems() is greater than 123, Sad otherwise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.