深究AngularJS中$sce的使用
為什么要要$sce?因?yàn)锳ngularJS里好些地方,比如路徑默認(rèn)是個(gè)字符串,不會(huì)認(rèn)為是路徑,從而訪問不到我們需要的東西,那么我們就可以通過$sce告訴angualrJS這個(gè)路徑,這樣是很安全滴。它有以下幾種:
$sce.trustAs(type,name); $sce.trustAsUrl(value); $sce.trustAsHtml(value); $sce.trustAsResourceUrl(value); $sce.trustAsJs(value);
1.trustAsResourceUrl
<html> <head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> 第一種方式:<br/> 有$sce處理:<audio ng-src="{{sceControl(formData.mediaUrl)}}" controls="controls">您的瀏覽器不支持html5</audio><br/> 無$sce處理:<audio ng-src="{{formData.mediaUrl}}" controls="controls">您的瀏覽器不支持html5</audio><br/><br/> 第二種方式:<br/> <audio ng-src="{{data.url}}" controls="controls">您的瀏覽器不支持html5</audio> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope,$sce){ //第一種方式數(shù)據(jù)源 $scope.formData={ "name":"視頻", "mediaUrl":"http://res.audiogroup.cn/res/upload/agx_ims_creative/audio/20160307/bpryk96.mp3"http://視頻路徑 }; $scope.sceControl = $sce.trustAsResourceUrl;//第一種處理方式 //第二種方式數(shù)據(jù)源 $scope.data={ "name":"視頻", "url":"http://res.audiogroup.cn/res/upload/agx_ims_creative/audio/20160307/bpryk96.mp3"http://視頻路徑 }; $scope.data.url = $sce.trustAsResourceUrl($scope.data.url);//第二種處理方式 }); </script> </body> </html>
2.trustAsHtml
<body> <div ng-app="myApp" ng-controller="myCtrl"> 未處理的: <div ng-repeat="item in formData"> {{item.name}} :{{item.htmlVal}} </div> <br/>處理過的:<button ng-click="look()">查看處理結(jié)果</button> <div ng-repeat="item in data"> {{item.name}} :<p ng-bind-html = "item.htmlVal"></p> </div> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope,$sce){ //未處理數(shù)據(jù)源 $scope.formData=[ {"name":"張春玲","htmlVal":"我是<span style='color:red;'>張春玲<span>"}, {"name":"sb","htmlVal":"我是<span style='color:red;'>sb<span>"} ]; //處理結(jié)果 $scope.look = function(){alert $scope.data=[ {"name":"張春玲","htmlVal":"我是<span style='color:red;'>張春玲<span>"}, {"name":"sb","htmlVal":"我是<span style='color:red;'>sb<span>"} ]; for(var i=0;i<$scope.data.length;i++){ $scope.data[i].htmlVal = $sce.trustAsHtml($scope.data[i].htmlVal); } }; }); </script> </body>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能
這篇文章主要介紹了Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能,對(duì)Angular.js services的學(xué)習(xí)有所幫助,需要的朋友可以參考下2018-07-07后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
這篇文章主要介紹了后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-07-07Angular應(yīng)用Bootstrap過程步驟邏輯詳解
這篇文章主要為大家介紹了Angular應(yīng)用Bootstrap過程步驟邏輯詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Angular.Js之Scope作用域的學(xué)習(xí)教程
這篇文章主要給大家分享了關(guān)于Angular.Js之Scope作用域的學(xué)習(xí)教程 ,文中通過多個(gè)示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例
本篇文章主要介紹了在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例
本篇文章主要介紹了angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02詳解Angular中的自定義服務(wù)Service、Provider以及Factory
本篇文章主要介紹了詳解Angular中的自定義服務(wù)Service、Provider以及Factory,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04