AngularJS ng-bind-html 指令
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 >
< 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 | 指定要執行的變量或表達式。 |