0

I have a statement where I need to assign a variable based on condition:

<b>{{ (varA.count > 1 ? {{'varB.status' | localizedString}} : '') }} </b>

where {{varB.status | localizedString}} filters 'varB.Status' with a localizedString specified in another place if for example varB.Status is "Test" for the chosen localization it would return "Test"

This gives me error of

Syntax Error: Token 'varB.status' is unexpected, expecting [:]
1
  • Just do <b>{{ (varA.count > 1 ? varB.status : '') }} </b> Commented Oct 10, 2014 at 15:02

3 Answers 3

2

You have syntax error, you do not need interpolation inside interpolation and that too between incomplete expression. Expression will be evaluated as properties against scope itself, assuming varB is also on the scope.

Just do:-

<b>{{ (varA.count > 1 ? varB.status : '') }} </b>

Demo

If you want to apply filter :-

<b>{{ (varA.count > 1 ? (varB.status | localizedString) : '') }} </b>

or even

<b>{{ (varA.count > 1 ? varB.status : '') | localizedString }} </b>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

<b>{{ (varA.count > 1 ? varB.status : '') }} </b>

Comments

0

The {{ and }} braces allow you to inject an Angular expression inside HTML.

 <b>outside {{ my_expression }} outside</b>
                 ^^^^^^^^ 

In the above the text inside the {{...}} will be converted to an Angular expression.

 <b>outside {{ my_expression }} outside</b>
     ^^^^^                       ^^^^^

In the above the text outside the {{...}} will remain unchanged.

You can also use the ngBind directive to assign the result of an expression to the body of a DOM element.

<b ng-bind="mY_expression"></b>

https://docs.angularjs.org/api/ng/directive/ngBind

To show the value of varB.status just write varA.count > 1 ? varB.status : '' like this.

<b>{{varA.count > 1 ? varB.status : ''}}</b>

Alternatively, the above can be shorten to just this.

<b>{{varA.count > 1 && varB.status}}</b>
         <!-- or -->
<b ng-bind="varA.count > 1 && varB.status"></b>

Or you could make the <b> optional.

<b ng-if="varA.count > 1">{{varB.status}}</b>

6 Comments

{{varA.count > 1 && varB.status}} you dont want to output false if condition does not match
@PSL would it write false or an empty string?
Using operation that way is confusing and generally gives desired output only when it is truthy. plnkr.co/edit/fRSPZi?p=preview
Last one is fine, but unwantedly creates 2 watches against one right?
@PSL true, but sometimes empty DOM elements can cause styling issues.
|

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.