【发布时间】:2013-12-10 21:28:51
【问题描述】:
我是 AngularJS 的新手,我觉得我只是触及了框架可能实现的表面。但是,我遇到了 sce.trustAsHtml 函数的问题。我正在运行 AngularJS 1.2.4。
在我的应用程序中,我正在使用 JSON 加载项目。这些项目使用指令显示在列表中。有时,我想将 HTML 注入检索到的内容中(例如,使链接可点击)。
我读过我可以使用 $sce.trustAsHtml 来允许绑定 html。但是,以下 sn-p 不起作用。我希望所有项目都替换为粗体文本“测试”,而是为每个项目显示<strong>Test</strong>。
有没有一种简单的方法可以让这个 sn-p 工作?
angular.directive('ngStream', function($timeout, $sce) {
var url = "getitems.json";
return {
restrict: 'A',
scope: {},
templateUrl: 'templates/app_item.html',
controller: ['$scope', '$http', function($scope, $http) {
$scope.getItems = function() {
$http.get(url,{}).success(function(data, status, headers, config) {
$scope.items = data;
});
}
}],
link: function(scope, iElement, iAttrs, ctrl) {
scope.getItems();
scope.$watch('items', function(newVal) { if (newVal) {
angular.forEach(newVal, function(vars,i) {
# Example html string for testing purposes.
var editedContent = '<strong>Test</strong>';
newVal[i].contentHtml = $sce.trustAsHtml(editedContent)
});
}});
},
}
});
【问题讨论】:
标签: javascript angularjs