gpt4 book ai didi

javascript - AngularJS 的 $sce.trustAsHtml 被忽略

转载 作者:行者123 更新时间:2023-12-02 07:32:25 25 4
gpt4 key购买 nike

我是 AngularJS 的新手,我觉得我只是触及了该框架可能实现的功能的皮毛。但是,我遇到了 sce.trustAsHtml 函数的问题。我正在运行 AngularJS 1.2.4。

在我的应用程序中,我使用 JSON 加载项目。这些项目使用指令显示在列表中。有时,我想将 HTML 注入(inject)到检索到的内容中(例如,使链接可点击)。

我读过我可以使用 $sce.trustAsHtml 来允许绑定(bind)中的 html。但是,以下代码段不起作用。我希望所有项目都替换为粗体文本“测试”,但它显示的是 <strong>Test</strong>对于每个项目。

有没有一种简单的方法可以使这个代码段起作用?

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

最佳答案

你的模板上有什么? $sce.trustAsHtml 必须与 ng-bind-html 一起使用,而不是普通的 ng-bind (或 {{}})

关于javascript - AngularJS 的 $sce.trustAsHtml 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com