angularjs显示html片段

ngBindHtml

<div ng-controller="ExampleController">
 <p ng-bind-html="myHTML"></p>
</div>
angular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.myHTML =
     'I am an <code>HTML</code>string with ' +
     '<a href="#">links!</a> and other <em>stuff</em>';
}]);

但是这个时候还是会报错

Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context.

$sce

使用trustAsHtml(value)对内容进行转换就可以了

        .controller('MissDetailController', ['$location', '$rootScope', '$scope', '$state', '$stateParams', '$sce', 'MissService',
            function($location, $rootScope, $scope, $state, $stateParams, $sce, MissService) {
                $scope.id = $stateParams.id || 1;
                MissService.get({ id: $scope.id }, function(resp) {
                    $scope.miss = resp.data;
                    $scope.miss.brief = $sce.trustAsHtml($scope.miss.brief);
                })
            }
        ]);

你开可以彻底关掉这个$sce检查, 不过不推荐

angular.module('myAppWithSceDisabledmyApp', []).config(function($sceProvider) {
  // Completely disable SCE.  For demonstration purposes only!
  // Do not use in new projects or libraries.
  $sceProvider.enabled(false);
});
posted @ 2017-09-16 14:47  CooMark  阅读(744)  评论(0编辑  收藏  举报