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?

4
  • 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) Commented Feb 10, 2015 at 5:07
  • Hi I changed the question a little bit to simplify it. I basically want to parse and replace variables with the values. Commented Feb 10, 2015 at 5:19
  • If you want to leverage both $compile and ng-bind-html, this question can help Commented Feb 10, 2015 at 5:25
  • I see. Thanks, the question is clearer now. The answer provided by @Arun should work Commented Feb 10, 2015 at 9:44

1 Answer 1

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

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

1 Comment

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.

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.