jamesdeath123 jamesdeath123 - 2 months ago 6
AngularJS Question

angular how to let html redner the scope variable as a string

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?

Answer

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