AngularJS ng-bind-html 指令
AngularJS 實例
綁定<p> 內的innerHTML 到變量myText:
<!DOCTYPE html><html><head><meta charset="utf-8"><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></head><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><p><b>注意:</b> 該實例包含了"angular-sanitize.js" 文件,該文件移除HTML 中的危險代碼。 </p></body></html>運行結果:
my name is:
John Doe
注意:該實例包含了"angular-sanitize.js" 文件, 該文件移除HTML 中的危險代碼。
定義和用法
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 | 指定要執行的變量或表達式。 |
以上就是AngularJS ng-bind-html 指令實例的詳細介紹,有需要的朋友可以參考下。