当前位置:AngularJS API / ng / 指令(directive) / ngBindhtml

ng

AngularJS 实例

绑定 <p> 内的 innerHTML 到变量 myText:

<script      src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script      src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

         <p ng-bind-html="myText"></p>

</div>

<script>
var app =      angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl",      function($scope) {
    $scope.myText = "My name is:      <h1>John Doe</h1>";
});
</script>

尝试一下 »

定义和用法

ng-bind-html 指令是通一个安全的方式将内容绑定到 HTML 元素上。

当你想让 AngularJS 在你的应用中写入 HTML,你就需要去检测一些危险代码。通过在应用中引入 "angular-santize.js" 模块,使用 ngSanitize 函数来检测代码的安全性。 in your application you can do so by running the HTML code through the ngSanitize function.


语法

<element ng-bind-html="expression"></element>

所有的 HTML 元素都支持该指令。


参数值

描述
expression 指定要执行的变量或表达式。