Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS'$sce.trustAsHtml被忽略_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS'$sce.trustAsHtml被忽略

Javascript AngularJS'$sce.trustAsHtml被忽略,javascript,angularjs,Javascript,Angularjs,我是AngularJS的新手,我觉得我只是在初步了解框架的可能性。但是,我在使用sce.trustAsHtml函数时遇到了问题。我正在运行AngularJS 1.2.4 在我的应用程序中,我使用JSON加载项目。这些项目使用指令显示在列表中。有时,我想将HTML注入检索到的内容中(例如,使链接可点击) 我已经读到我可以使用$sce.trustAsHtml在绑定中允许html。但是,以下代码段不起作用。我希望所有项目都替换为粗体文本“test”,但它会为每个项目显示test 有没有一种简单的方法可

我是AngularJS的新手,我觉得我只是在初步了解框架的可能性。但是,我在使用sce.trustAsHtml函数时遇到了问题。我正在运行AngularJS 1.2.4

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

我已经读到我可以使用$sce.trustAsHtml在绑定中允许html。但是,以下代码段不起作用。我希望所有项目都替换为粗体文本“test”,但它会为每个项目显示
test

有没有一种简单的方法可以让这个片段工作

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)
                });
            }});
        },
    }
});
angular.directive('ngStream',函数($timeout,$sce){
var url=“getitems.json”;
返回{
限制:“A”,
作用域:{},
templateUrl:'templates/app_item.html',
控制器:['$scope','$http',函数($scope,$http){
$scope.getItems=函数(){
$http.get(url,{}).success(函数(数据、状态、标题、配置){
$scope.items=数据;
});
}
}],
链接:功能(范围、IELENT、iAttrs、ctrl){
scope.getItems();
作用域.$watch('items',函数(newVal){if(newVal){
forEach(newVal,function(vars,i){
#用于测试目的的html字符串示例。
var editedContent='测试;
newVal[i].contentHtml=$sce.trustAsHtml(editedContent)
});
}});
},
}
});

模板上有什么
$sce.trustAsHtml
必须与
ng bind html
一起使用,而不是正常的
ng bind
(或
{}

谢谢!我的模板以前是:
{{item.contentHtml}
。通过将其更改为
,它工作了。您知道使用ng bind html(不使用不必要的span标记)的更干净的方法吗?好吧,这是使用
ng bind html
的唯一方法。您通过强调必须与
ng bind html一起使用而救了我一天