我想在angularjs 1.2中使用unsafe-html。虽然不带html的过滤器可以工作,但是带html的却不能。我的做法:
我在html头部添加了angular-sanitize:
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-sanitize.js"></script>
我的Angular模块:
var myApp = angular.module('myApp', ['ngSanitize'])
.filter('convertState', function ($sce) {
return function (state) {
if (state == 1) {
return $sce.trustAsHtml("<strong>" + state + "</strong> special state");
}
else {
return $sce.trustAsHtml("<strong>"+state + "</strong> normal state");
}
}
});
我的Html:
<td><span ng-bind-html="f.state | convertstate"></span></td>
编辑:将ng-bind-html-unsafe
更新为ng-bind-html
ngSanitize
时,并不总是需要使用$sce.trustAsXXX()
(实际上在这种情况下并不需要)。这就是净化的全部目的。 - gkalpak