深入理解AngularJS中的ng-bind-html指令
前言
在為html標(biāo)簽綁定數(shù)據(jù)的時(shí),如果綁定的內(nèi)容是純文本,你可以使用{{}}或者ng-bind。但在為html標(biāo)簽綁定帶html標(biāo)簽的內(nèi)容的時(shí)候,angularjs為了安全考慮,不會(huì)將其渲染成html,而是將其當(dāng)做文本直接在頁(yè)面上展示。
先來看一個(gè)例子
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="js/angular.min.js"></script>
<script>
angular.module("myapp", []).controller("MyController", function ($scope) {
$scope.content = "<h1>Hello world.</h1>";
$scope.txt = "Hello txt world";
});
</script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController">
{{content}}
<div ng-bind="content"></div>
</div>
</body>
</html>
輸出

ng-bind-html指令
<div ng-bind-html="content"></div>
這時(shí)就會(huì)出現(xiàn)安全的錯(cuò)誤,如圖:

但可以通過引入下面的模塊,自動(dòng)檢測(cè)html的內(nèi)容是否安全
<script src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js"></script>
<script>
angular.module("myapp", ["ngSanitize"]).controller("MyController", function ($scope) {
$scope.content = "<h1>Hello world.</h1>";
$scope.txt = "Hello txt world";
});
</script>
這時(shí)刷新預(yù)覽

所以
ng-bind-html 指令是通一個(gè)安全的方式將內(nèi)容綁定到 HTML 元素上。
當(dāng)你想讓 AngularJS 在你的應(yīng)用中寫入 HTML,你就需要去檢測(cè)一些危險(xiǎn)代碼。通過在應(yīng)用中引入 "angular-santize.js" 模塊,使用 ngSanitize 函數(shù)來檢測(cè)代碼的安全性。 in your application you can do so by running the HTML code through the ngSanitize function.
另外一種處理方式
通過自定義過濾器,將帶html標(biāo)簽的內(nèi)容都當(dāng)成安全的進(jìn)行處理。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="js/angular.min.js"></script>
<!--<script src="http://apps.bdimg.com/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js"></script>-->
<script>
angular.module("myapp", []).controller("MyController", function ($scope) {
$scope.content = "<h1>Hello world.</h1>";
$scope.txt = "Hello txt world";
}).filter("safeHtml", function ($sce) {
return function (input) {
//在這里可以對(duì)加載html渲染后進(jìn)行特別處理。
return $sce.trustAsHtml(input);
};
});
</script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController">
{{content}}
<div ng-bind="content"></div>
<!--<div ng-bind-html="content"></div>-->
<div ng-bind-html="content|safeHtml"></div>
</div>
</body>
</html>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
angular inputNumber指令輸入框只能輸入數(shù)字的實(shí)現(xiàn)
這篇文章主要介紹了angular inputNumber指令輸入框只能輸入數(shù)字的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
angular.js指令中transclude選項(xiàng)及ng-transclude指令詳解
這篇文章主要研究一下如何使用ng-transclude指令,以及指令的transclude選項(xiàng)的相關(guān)資料,文中介紹的非常詳細(xì),并給出了完整的示例代碼大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-05-05
詳解Angular 4.x NgTemplateOutlet
這篇文章主要介紹了詳解Angular 4.x NgTemplateOutlet,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能,結(jié)合實(shí)例形式分析了AngularJS簡(jiǎn)單控制頁(yè)面元素顯示與隱藏的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
詳解angularjs popup-table 彈出框表格指令
本篇文章主要介紹了angularjs popup-table 彈出框表格指令,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
AngularJS 在同一個(gè)界面啟動(dòng)多個(gè)ng-app應(yīng)用模塊詳解
這篇文章主要介紹了AngularJS 在同一個(gè)界面啟動(dòng)多個(gè)ng-app應(yīng)用模塊詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
Angular 4依賴注入學(xué)習(xí)教程之簡(jiǎn)介(一)
依賴注入式AngularJS的重要特性之一,依賴注入簡(jiǎn)化了Angular解析模塊/組件之間依賴的過程。下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
angularjs實(shí)現(xiàn)下拉列表的選中事件示例
本篇文章主要介紹了angularjs實(shí)現(xiàn)下拉列表的選中事件示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

