Parsing a string containing data bindings in AngularJS

I have a processed backend template that returns a JSON object that contains a string that requires some dynamic data bindings, for example ...

sampleLogic = { "1": "Sample static text and some {{ dynamic_text }}." } 

By default, the string is escaped, what is 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> 

Violin: http://jsfiddle.net/RzPM3/

+6
source share
1 answer

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

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

Demo

+5
source

Source: https://habr.com/ru/post/952041/


All Articles