AngularJS中ng-bind-html指令的功能是什么-創(chuàng)新互聯(lián)

這篇文章給大家介紹AngularJS中ng-bind-html指令的功能是什么,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)公司成立與2013年,先為仁和等服務(wù)建站,仁和等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為仁和企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

前言

在為html標(biāo)簽綁定數(shù)據(jù)的時,如果綁定的內(nèi)容是純文本,你可以使用{{}}或者ng-bind。但在為html標(biāo)簽綁定帶html標(biāo)簽的內(nèi)容的時候,angularjs為了安全考慮,不會將其渲染成html,而是將其當(dāng)做文本直接在頁面上展示。

先來看一個例子

<!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 = "<h2>Hello world.</h2>";
 $scope.txt = "Hello txt world";
 });
 </script>
</head>
<body ng-app="myapp">
 <div ng-controller="MyController">
 {{content}}
 <div ng-bind="content"></div> 
 </div>
</body>
</html>

輸出

AngularJS中ng-bind-html指令的功能是什么

ng-bind-html指令

 <div ng-bind-html="content"></div>

這時就會出現(xiàn)安全的錯誤,如圖:

AngularJS中ng-bind-html指令的功能是什么

但可以通過引入下面的模塊,自動檢測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 = "<h2>Hello world.</h2>";
 $scope.txt = "Hello txt world";
 });
 </script>

這時刷新預(yù)覽

AngularJS中ng-bind-html指令的功能是什么

所以

ng-bind-html 指令是通一個安全的方式將內(nèi)容綁定到 HTML 元素上。

當(dāng)你想讓 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.

另外一種處理方式

通過自定義過濾器,將帶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 = "<h2>Hello world.</h2>";
  $scope.txt = "Hello txt world";
 }).filter("safeHtml", function ($sce) {
  return function (input) {
  //在這里可以對加載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>

關(guān)于AngularJS中ng-bind-html指令的功能是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站bm7419.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前標(biāo)題:AngularJS中ng-bind-html指令的功能是什么-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article42/cdiiec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站定制開發(fā)、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站品牌網(wǎng)站設(shè)計、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化