Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question
    
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 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. –  Behlül Feb 10 at 5:19
    
If you want to leverage both $compile and ng-bind-html, this question can help –  Rebornix Feb 10 at 5:25
    
I see. Thanks, the question is clearer now. The answer provided by @Arun should work –  David Parlevliet Feb 10 at 9:44

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.