1

I have an application in which the value of an html element comes from server as a template. I'm trying to assign this template, which is basically a string and get the variables in template bound to controller but angular doesn't bind the variables.

To be more clear, I have a textarea like this

  <textarea class="someclass" placeholder="${fbPostData.name}"
            ng-model="fbPostMessage">
  </textarea>

In my controller, I assign fbPostMessage to be:

$scope.fbPostMessage = "Join {{userFirstName}}. Subscribe today to have fun!"

After these steps in my view I don't see the variables replaced with their values, userFirstName is not replaced by its value.

I've tried $compile and it gave error. It probably expects html tags. How can I achieve what I want?

Behlül
  • 3,412
  • 2
  • 29
  • 46
  • It's really hard to give you an answer from all that you've provided. Instead, I'd say to 1) ensure that the data is being assigned in the correct scope, you can do that by assigning something with a constant value `$scope.test_value = "test";` then replacing `ng-model="test_value"` 2) ensure that angular knows about the update by doing `$scope.$digest();` (this should be a last resort) – David Parlevliet Feb 10 '15 at 05:07
  • Hi I changed the question a little bit to simplify it. I basically want to parse and replace variables with the values. – Behlül Feb 10 '15 at 05:19
  • If you want to leverage both `$compile` and `ng-bind-html`, this [question](http://stackoverflow.com/questions/19726179/how-to-make-ng-bind-html-compile-angularjs-code) can help – Rebornix Feb 10 '15 at 05:25
  • I see. Thanks, the question is clearer now. The answer provided by @Arun should work – David Parlevliet Feb 10 '15 at 09:44

1 Answers1

3

Instead of compile use interpolate to compile your before it display.

$scope.userFirstName = "Hello";
$scope.fbPostData={
   name : "Placeholder"
}
$scope.fbPostMessage = $interpolate("Join {{userFirstName}}. Subscribe  today to have fun!")($scope)

Try this example

http://plnkr.co/edit/uqpe32Gy0IfxWGLoPdLX?p=preview

Arun
  • 91
  • 3
  • Although I was looking for a solution which would bind the variables inside the string, watch the variables and update the string when their values change this is also good. So I'm accepting this answer. – Behlül Feb 11 '15 at 17:54