AngularJS ng-bind-html 指令

AngularJS 參考手冊 AngularJS 參考手冊


AngularJS 實例

綁定 <p> 內(nèi)的 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 指令是通一個安全的方式將內(nèi)容綁定到 HTML 元素上。

當你想讓 AngularJS 在你的應(yīng)用中寫入 HTML,你就需要去檢測一些危險代碼。通過在應(yīng)用中引入 "angular-santize.js" 模塊,使用 ngSanitize 函數(shù)來檢測代碼的安全性。 in your application you can do so by running the HTML code through the ngSanitize function.


語法

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

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


參數(shù)值

描述
expression 指定要執(zhí)行的變量或表達式。

AngularJS 參考手冊 AngularJS 參考手冊