1

Say I have the following line in a controller hmmCtrl:

$rootScope.value = 1;
$scope.hmm = "{{$root.value}}"

And in html, if I have:

<section ng-controller="hmmCtrl">
 {{hmm}}
</section>

Currently it displays:

{{$root.value}}

but I actually want to see the value of $root.value:

1

In the long run I plan to put the $root.value in a json file that is going to be parsed by the hmmCtrl.

How can I make this happen?

1
  • From a quick look it seems You have a typo it should be $scope.hmm = $rootScope.value not root Commented Sep 30, 2016 at 21:56

2 Answers 2

1

So what you can do is, you can write a inside a controller which can return your interpolation expression evaluated value

Code

//inject `$interpolate` inside controller function before using it.
$scope.evaluateValue = function(expr){
   return $interpolate(expr)($scope);
}

Markup

<section ng-controller="hmmCtrl">
  {{evaluateValue(hmm)}}
</section>

Other way

<section ng-controller="hmmCtrl" ng-bind="evaluateValue(hmm)">
</section>

Demo Here

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

6 Comments

hmm, this prints out evaluateValue(hmm) to me - I put the $scope.eveluateValue inside hmmCtrl.
Hmm, the updated works as the value can be shown now, but the console gives error: Error: f is undefined h@localhost:81/js/angular_core/angular.min.v.1.4.5.js:94:218 $scope.evaluate@localhost:81/js/controllers/menu_controllers.js:11:11 where menu_controllers.js:11 is the line of return $interpolate(expr)($scope); any ideas?
Have yoi injected $interpolate service in controller?
yes - the value is correctly interpolated; just with the f is undefined error.
Could you please create a plunkr?
|
0

Change your html-Code from

<section ng-controller="hmmCtrl">
    {{hmm}}
</section>

to

<section ng-controller="hmmCtrl">
    {{value}}
</section>

$scope inherites from $rootScope, meaning $scope also has the "value"-variable...

2 Comments

Although your answer is right that $scope inherits from $rootScope and it also have "value", it doesn't solve the problem of parsing "{{$root.value}}" or "{{$scope.value}}" properly, which is the real problem I tried to ask..
Oh yes, I didn't know where $root came from and misunderstood it as $rootScope. My bad^^

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.