分析包含在AngularJS数据绑定字符串 [英] Parse a string that contains data bindings in AngularJS

查看:144
本文介绍了分析包含在AngularJS数据绑定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 }}." 
}

默认情况下,字符串被转义,什么是在角转换dynamic_text的最佳方式绑定到$ scope.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>

小提琴:
http://jsfiddle.net/RzPM3/

推荐答案

您可以使用的 $插值模块轻松实现像这样

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);

<大骨节病> 演示

这篇关于分析包含在AngularJS数据绑定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆