angular 1.2以后(或更早?)移除了ng-bind-html-unsafe
,那么我要用這個directive來綁定html代碼怎么辦?隨便一測試,它是不支持把html直接傳給它的:
//html
<p ng-bind-html="m"></p>
//js
$scope.m="<b>text</b>";
//error:
[$sce:unsafe] Attempting to use an unsafe value in a safe context.
參考這篇文章,得到解決。我選擇的是直接做一個過濾器:
//js
app.filter('to_trusted', ['$sce', function ($sce) {
return function (text) {
return $sce.trustAsHtml(text);
};
}]);
app.controller...{
$scope.m="<b>text</b>"
}
//html:
<p ng-bind-html="m | to_trusted"></p>