0

I am trying to use expression with in expression as below.

is it possible to resolve expression within expression with double braces?

<body>

<div ng-app="" ng-init="names=[
{name:'Jani',exp:'{{10>20}}'},
{name:'Hege',exp:'{{abc>20}}'},
{name:'Kai',exp:'{{10==20}}'}]">

<p>Looping with objects:</p>
<ul>
  <li ng-repeat="x in names">
  {{ x.name + ', ' + {{x.exp}} }}</li>
</ul>

</div>

</body>
1
  • Check this out Link Answer Commented Feb 27, 2017 at 21:09

2 Answers 2

0

you can evaluate via ng-if statment OR Via ternary expression

EX: {{10 > 20 ?"Yup":"No"}}

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

Comments

0

It is best practice to avoid using ng-init in this manner. You should handle all variable initialization inside of the controller. You also shouldn't be trying to evaluate arbitrary expressions in angular, you should know what you're trying to do. If you provide more information on what you are trying to accomplish, we could help with that.

That being said, you can evaluate an arbitrary string as an angular expression using $eval.

{{ x.name + ', ' + $ctrl.$eval(ex.exp) }}

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$eval

4 Comments

Thanks Fusion , I just ng-init to explain issue which I am facing, in actual code what I am trying , the data comes from controller with http get service, I am stuck resolving expression inside expression
Why are you having these expressions? It would probably be best to perform the logic when you receive the data from the service.
Problem is, I am using attribute of a directive and trying to resolve the expression in ng-class with expression in expression .. something like ng-class={'active': {{sal > 10}} }
Does the inside expression change? You could use the onInit function to evaluate the expression in the controller

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.