解析包含AngularJS中的数据绑定的字符串

时间:2021-01-31 17:03:51

I have a backend rendered template that returns a JSON object that contains a string that needs some dynamic data bindings for example...

我有一个后端渲染模板,它返回一个JSON对象,其中包含一个需要一些动态数据绑定的字符串,例如......

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?

默认情况下,字符串是转义的,将angular_text转换为绑定到$ 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/

1 个解决方案

#1


5  

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

您可以使用$ interpolate模块并轻松实现这一点

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

DEMO

#1


5  

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

您可以使用$ interpolate模块并轻松实现这一点

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

DEMO