Parse a string that contains data bindings in AngularJS

angularjs, javascript

Solution

You can use $interpolate module and easily achieve it like this

var dynamic_text = {
    'dynamic_text': "dynamic text woooot"
};
$scope.copy = $interpolate(sampleLogic['1'])(dynamic_text);

DEMO

Problem

I have a backend rendered template that returns a JSON object that contains a string that needs some dynamic data bindings for example... ``` sampleLogic = { "1": "Sample static text and some {{ dynamic_text }}." } ``` By default the string is escaped, what's the best way in angular to convert dynamic_text to bind to $scope.dynamic_text? JS: ``` var sampleLogic = { "1": "Sample static text and some {{ dynamic_text }}." }; function parseMe($scope) { $scope.copy = sampleLogic['1']; $scope.dynamic_text = "dynamic text woooot"; } ``` HTML: ``` <div ng-app> <div ng-controller="parseMe"> <div ng-bind-html-unsafe="copy"></div> </div> </div> ``` Fiddle: http://jsfiddle.net/RzPM3/

Original source