我想不要在Angular应用程序中转义HTML标记

时间:2014-09-09 08:58:23

标签: javascript angularjs

我是AngularJS的新手。我希望 NOT 转义HTML标记,所以

  1. 包括 angular-sanitize.js
  2. 写下面的代码:
  3. <!-- <div>{{article.content}}</div> -->
    <div ng-bind-html="{{article.content}}"></div>
    

    但是没有任何东西被渲染出来。

2 个答案:

答案 0 :(得分:3)

你不需要大括号:

<div ng-bind-html="article.content"></div>

你还需要确保角度知道它在你的控制器中是安全的:

$sce.trustAsHtml($scope.article.content);

答案 1 :(得分:2)

我认为这会对你有所帮助。

plunker

var app = angular.module('app', ['ngSanitize']);
app.controller('appCtrl', ['$scope', '$sce', ctrl]);

function ctrl($scope, $sce) {
  $scope.getContent = function() {
    return "shohel rana";
  };
}
<body ng-controller="appCtrl">
  <h1>Hello Plunker!</h1>
  <div ng-bind-html="getContent()"></div>
</body>