0

I have, in my angular controller, a text like "For {{selDays}} days" which I want to bind to the view and have "selDays" changing.

I looked into $compile, but I saw that's mainly for use in directives.

Any ideas?

2
  • Is it required that the text is in a variable? Commented Apr 11, 2014 at 7:52
  • yep. I have a slider which changes that number. Commented Apr 11, 2014 at 8:43

2 Answers 2

1

$interpolate() can be used to interpolate the expression...

$scope.message = $interpolate("For {{selDays}} days")($scope);

But it'll only do it once. Use a watch if you need to reflect further changes to selDays...

var interpolatedMessage = $interpolate("For {{selDays}} days");
$scope.$watch('selDays', function () {
    $scope.message = interpolatedMessage($scope);
});

JsBin: http://jsbin.com/copoweyo/2/edit?html,js,output

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

1 Comment

thanks, I didn't knew about $interpolate. Did the job perfectly!
0

Why don't you write "For {{selDays}} days" in your view. Then you write selDays var in the controller scope. Each time you will modifiy the variable, the display in the view will change (data binding)

ex.

VIEW: For {{selDays}} days

CONTROLLER: $scope.selDays = 42;

1 Comment

I need to use the same string in more than one place. So duplicating it would mean more work when changing it

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.