13

我正在显示一个包含 HTML 代码的字符串:

<div style="font-size: 14px" ng-bind="currentBook.description"></div>

put 它显示 HTML 代码而不是解释元素:

在此处输入图像描述

当我使用ng-bindandng-bind-unsafe时,它什么也没显示。

如何获取要解析的 HTML?

附录

我添加了对 sanitize 的引用,但ng-bind仍然ng-bind-unsafe没有显示:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-route.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>

好的,我添加了 ngSanitize var app = angular.module('app', ['ngSanitize']);,现在它可以工作了。

4

2 回答 2

19

看起来你错过了ngSanitize请看下面的演示

你可以在这里找到更多

https://docs.angularjs.org/api/ngSanitize/service/ $sanitize

var app = angular.module('app', ['ngSanitize']);

app.controller('firstCtrl', function($scope, $sce) {
  $scope.currentBook = {
    description: "<p>some description</p>"

  };
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-sanitize.js"></script>

<body ng-app="app">
  <div ng-controller="firstCtrl">
    <div style="font-size: 14px" ng-bind-html="currentBook.description"></div>
  </div>
</body>

于 2015-01-10T20:09:13.800 回答
3

打电话申请

    $scope.$apply(function () {
            $scope.currentBook.description = $sce.trustAsHtml(html);
        });
于 2016-06-02T20:52:32.530 回答