我是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 = 'Test'; newVal[i].contentHtml = $sce.trustAsHtml(editedContent) }); }}); }, } });
tungd.. 7
你的模板上有什么?$sce.trustAsHtml
必须使用ng-bind-html
而不是正常ng-bind
(或{{}}
)