2

I'm lost, could you help me?

I have a value in the $scope which is retrieved from JSON file:

in Controller:

$scope.scopeTitle = title;

in JSON file:

"title": "fruit.ID || 'Empty'"

The fruit.ID is retrieved from backend, and I want to display it's ID in my HTML.

Everything is works fine if in my HTML I do sth like:

<span class="navbar-brand"  data-ng-bind="fruit.ID || 'Empty'" />

the result on the HTML is:

FRUIT/12 or Empty <- and it's good

If I want to pass it via $scope, like this:

<span class="navbar-brand"  data-ng-bind='scopeTitle' />

it's not working, the result on the HTML is:

fruit.ID || 'Empty' <- it's not good

My HTML is generated in template in directive and I'm using a $compile function to compile the HTML code.

How to write the ng-bind expression properly to achieve the result via $scope?

kamyk
  • 295
  • 8
  • 25
  • Can you try using `$eval` to see if it would work. It's almost impossible to evaluate a string in Angular expressions. You can check out `$eval` [here](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$eval) – hisabimbola Jul 23 '15 at 12:40

2 Answers2

1

try to use $scope.$eval in your controller and use this to declare scopeTitle

$scope.scopeTitle = $scope.$eval(title);

**update : * you can see that you were assigning "fruit.ID || 'Empty'" as a string as an expression so you need to use $eval

and why not using something like this

<span class="navbar-brand">{{scopeTitle}}</span>
Ahmed Eid
  • 1,145
  • 8
  • 9
  • There are problems with the `$eval`. I'm getting an error: `Unknown provider: $evalProvider <- $eval`, but I'm injecting the `$eval` into my Controller. `I can't use just {{scopeTitle}}` because it won't bind from backend (I tried it also and it won't work) – kamyk Jul 23 '15 at 13:30
  • sorry dude my bad ,, I fixed it – Ahmed Eid Jul 23 '15 at 21:30
  • it's not a service it's a function in scope ,, I wrote that by mistake – Ahmed Eid Jul 23 '15 at 21:31
  • It working great! That's what I want. muchas gracias! :D – kamyk Jul 24 '15 at 06:32
-2

try

data-ng-bind={{scopeTitle}}
depperm
  • 10,606
  • 4
  • 43
  • 67
Mansi Parekh
  • 519
  • 2
  • 13