我想在 angularjs 1.2中 使用unsafe-html 。没有html的过滤器可以工作,而html则不能。我做的事:
我在我的html头中添加了angular-sanitize:
<script src="~/Scripts/angular.js"></script> <script src="~/Scripts/angular-sanitize.js"></script>
我的角度模块:
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
ng-bind-html-unsafe
ng-bind-html
ng-bind-html-unsafe已在Angular 1.2中删除。由于您正确地清理了输入内容,因此应该使用ng-bind-html。
示例:http://plnkr.co/edit/0bHeXrarRP7IAciqAYgM?p = preview