我正在使用Orchard CMS和AngularJS 1.2.20创建一个网站,我尝试渲染HTML代码,但我收到此错误
Error: [$sce:unsafe] http://errors.angularjs.org/1.2.20/$sce/unsafe
v/<@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:6:450
be/this.$get</e@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:117:34
be/this.$get</<.getTrusted@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:118:327
@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:120:71
kd</</<@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:191:341
Zd/this.$get</k.prototype.$digest@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:109:170
Zd/this.$get</k.prototype.$apply@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:112:171
h@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:72:452
w@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:77:347
we/</z.onreadystatechange@http://www.angularjshub.com/code/angularjs/repository/angular-1.2.20/angular.min.js:78:420
这是我的剧本:
var firstController = function ($scope, $http, $interval, $sce) {
$http.get(apiUrl + "/Authors)
.success(function (data, status, header, config) {
$scope.names = data;
});
$scope.renderHtml = function (html_code) {
return $sce.trustAsHtml(html_code);
};
这是我的观点:
<div ng-bind-html="renderHtml(authors.Photo)"></div>
答案 0 :(得分:1)
发生这种情况是因为您没有使用$sce.trustAsHtml
将html标记为安全,我认为您已完美地完成了此操作。你可以创建一个像这样的简单小提琴来重现它。
HTML代码:
<h2>Rendering HTML with angularJS</h2>
<div ng-app="angularApp" ng-controller="appController">
<div ng-bind-html="renderHtml(content)"></div>
</div>
JS代码:
var app = angular.module("angularApp", []);
app.controller("appController", function($scope, $sce){
$scope.content = "This text is <em>html capable</em> meaning you can have <a href=\"#\">all</a> sorts <b>of</b> html in here.";
$scope.renderHtml = function(html){
return $sce.trustAsHtml(html);
};
});
答案 1 :(得分:0)
好的,我发现了我的问题。我有一个div:
<div ng-bind-html="(authors.Name)"></div>
我没有调用renderHtml函数。