我是AngularJS的新手。我想不要转义HTML标记,并编写下面的代码:
<script>
angular.module('plunker', ['ngSanitize'])
.controller('MainCtrl', ['$scope', '$sce',
$scope.deliberatelyTrustDangerousSnippet = function() {
return $sce.trustAsHtml('<strong>foo</strong>');
};
])
</script>
<div ng-controller="MainCtrl">
<div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div>
</div>
代码也在这里:http://plnkr.co/edit/uqYNaoj2S8qGx4tMACcu
谢谢你的善意。
答案 0 :(得分:0)
您的整个控制器块都不正确。
而不是
.controller('MainCtrl', ['$scope', '$sce',
$scope.deliberatelyTrustDangerousSnippet = function() {
return $sce.trustAsHtml('<strong>foo</strong>');
};
])
应该是
angular.module('plunker', ['ngSanitize'])
.controller('MainCtrl', ['$scope', '$sce', function($scope, $sce){
$scope.deliberatelyTrustDangerousSnippet = function() {
return $sce.trustAsHtml('<strong>foo</strong>');
}
}]);
您没有为初学者宣布function()
- 这是您注入$scope
和$sce
的地方
这是更新的Plunkr