如何Angularjs1.3在頁面中輸出帶Html標記的文本
基於安全考慮,Angularjs不允許用ng-bind或者{{}}的方法輸出html文本。
在實際的應用中,比如信息管理系統,用在線編輯器編輯出來的文章都帶有html標記,這種情況下可以用ng-bind-html將其輸出到前台頁面。
1、在前台頁面中包含sanitize.js
<script type="text/javascript" src="webjars/angular-sanitize/1.3.11/angular-sanitize.min.js"></script>
2、在mode、和controller增加對html文本的安全過濾
angular.module('scenceApp',['ui.router','ngResource','ngSanitize','restangular']) .controller('scenceViewController',function($scope,Restangular,$stateParams, $sce){ Restangular.one("scences",$stateParams.id).get(). then(function(data) { $scope.scence = data; $scope.scence.info = $sce.trustAsHtml(data.info); //對info字段進行安全過濾 }); })
3、在前台頁面中用ng-bind-html綁定
<div ng-bind-html="scence.info"></div>