AngularJS中run方法的巧妙運(yùn)用
前言
AngularJS是google在維護(hù),其在國(guó)外已經(jīng)十分火熱,可是國(guó)內(nèi)的使用情況卻有不小的差距,參考文獻(xiàn)/網(wǎng)絡(luò)文章也很匱乏。網(wǎng)上關(guān)于AngularJS中run方法的介紹也比較少,本文就主要總結(jié)了關(guān)于AngularJS中run方法的巧妙運(yùn)用,感興趣的朋友們可以一起來學(xué)習(xí)學(xué)習(xí)。
一、瀏覽器判斷
在angular做微信應(yīng)用的時(shí)候,有時(shí)候我們也想把相同一份代碼運(yùn)行在非微信的瀏覽器上,這時(shí)候我們可以在angular的run上寫點(diǎn)東西實(shí)現(xiàn)~
例如asw.run
函數(shù)里執(zhí)行定義一個(gè)$rootScope.isWeiXinLogin
的函數(shù)
.run(['$rootScope', '$route', '$window', '$location', 'Position', '$cookies', 'Request', '$cookieStore', function($rootScope, $route, $window, $location, position, $cookies, request, $cookieStore) { //非微信的登陸 $rootScope.isWeiXinLogin = function() { //判斷是否微信登陸 var ua = window.navigator.userAgent.toLowerCase(); //console.log(ua); //mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko) version/9.0 mobile/13b143 safari/601.1 if (ua.match(/MicroMessenger/i) == 'micromessenger') { console.log(" 是來自微信內(nèi)置瀏覽器"); return true; } else { console.log("不是來自微信內(nèi)置瀏覽器"); return false; } }; ]);
這樣它能在應(yīng)用的其他部分之前提前被執(zhí)行,然后根據(jù)$rootScope.isWeiXinLogin
的返回我們可以在不同的視圖或者控制器有效的進(jìn)行判斷是否為微信瀏覽器
angular.module('autumnswind').controller('OrderCtrl', ['$rootScope', '$scope', 'Request', '$cookies', '$window', '$routeParams', '$location', 'Tool', function($rootScope, $scope, request, $cookies, $window, $routeParams, $location, tool) { if ($rootScope.isWeiXinLogin()) { ... } } ]);
二、登陸判斷
在run里面寫登陸判斷是一種不錯(cuò)的方案,例如下面我寫的這段,配合cookie和我上面的瀏覽器判斷,當(dāng)我加載頁(yè)面的時(shí)候我就可以調(diào)用$rootScope.goLogin
方案來判斷是否這個(gè)路由所在的視圖為登陸,如果有這個(gè)合法cookie就讓它繼續(xù)運(yùn)行,不然則返回login頁(yè)面進(jìn)行登陸~
$rootScope.goLogin = function(replace) { if ($rootScope.isWeiXinLogin()) { if (!replace) { $cookieStore.remove('loginBack'); delete $cookies.loginBack; $location.path('login'); } else { $cookies.loginBack = $location.path(); $location.path('login').replace(); } } else { $cookieStore.remove('loginBack'); delete $cookies.loginBack; $location.path('loginWebapp'); } };
三、白名單設(shè)置
曾經(jīng)寫過一個(gè)這樣的函數(shù)來實(shí)現(xiàn)路由的參數(shù)判斷,來設(shè)置白名單,那時(shí)候這個(gè)函數(shù)還放在全局變量里面~其實(shí)回頭想想算是不大好的方法
var getParam = function(name) { var search = document.location.search; var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g"); var matcher = pattern.exec(search); var items = null; if (null != matcher) { try { items = decodeURIComponent(decodeURIComponent(matcher[1])); } catch (e) { try { items = decodeURIComponent(matcher[1]); } catch (e) { items = matcher[1]; } } } return items; }; //這個(gè)是根據(jù)路由name來決定進(jìn)入那個(gè)parts window.cats = getParam('AutumnsWind');
后來改進(jìn)了下面這個(gè)簡(jiǎn)單的例子,就可以不用用上面那句代碼來實(shí)現(xiàn)了
$rootScope.$on('$routeChangeSuccess', function() { var route = window.location.href; if (route.indexOf('/hello/') != -1 && route.indexOf('/autumnswind/') != -1) { window.AutumnsWindShareUrl = window.location.href; } else if (route.indexOf('#/index') != -1) { window.AutumnsWindShareUrl = window.location.href; } else if (route.indexOf('#/asw'scat/') != -1) { window.AutumnsWindShareUrl = window.location.href; } else { //跳轉(zhuǎn)下載頁(yè)面 window.AutumnsWindShareUrl = '~autumns~.cn'; } );
上面我們根據(jù)路由發(fā)生的變化進(jìn)行白名單的設(shè)置,復(fù)雜點(diǎn)的話可以運(yùn)用一下正則,這樣就能很好的過濾我們禁止的url,由于例子就不寫這么復(fù)雜啦~
四、設(shè)置公共參數(shù)
這個(gè)其實(shí)就不用寫例子了,因?yàn)樯厦娴睦右菜闶沁@個(gè)的一部分吧~
總結(jié)
以上就是關(guān)于Angular中run方法巧妙運(yùn)用的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
- angularJS中$apply()方法詳解
- 使用AngularJS來實(shí)現(xiàn)HTML頁(yè)面嵌套的方法
- angularjs 處理多個(gè)異步請(qǐng)求方法匯總
- 在AngularJS中使用AJAX的方法
- 使用AngularJS處理單選框和復(fù)選框的簡(jiǎn)單方法
- AngularJS中取消對(duì)HTML片段轉(zhuǎn)義的方法例子
- Jquery和angularjs獲取check框選中的值的方法匯總
- angularJS 中$scope方法使用指南
- 簡(jiǎn)介AngularJS中使用factory和service的方法
- AngularJS Module方法詳解
相關(guān)文章
angular使用post、get向后臺(tái)傳參的問題實(shí)例
本篇文章主要介紹了angular使用post、get向后臺(tái)傳參的問題實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05AngularJS使用自定義指令替代ng-repeat的方法
這篇文章主要介紹了另一種即具有與ng-repeat一樣處理大量數(shù)據(jù)的綁定的功能,又具有超高性能的自定義方法,有需要的小伙伴們可以參考借鑒,下面來一起看看吧。2016-09-09AngularJS ionic手勢(shì)事件的使用總結(jié)
本篇文章主要介紹了AngularJS手勢(shì)事件的使用總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08angular.js和vue.js中實(shí)現(xiàn)函數(shù)去抖示例(debounce)
這篇文章主要介紹了angular.js和vue.js中實(shí)現(xiàn)函數(shù)去抖示例(debounce),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01基于AngularJS拖拽插件ngDraggable.js實(shí)現(xiàn)拖拽排序功能
ngDraggable.js是一款比較簡(jiǎn)單實(shí)用的angularJS拖拽插件,借助于封裝好的一些自定義指令,能夠快速的進(jìn)行一些拖拽應(yīng)用開發(fā)。本文先從基本概念入手,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-04-04AngularJS通過ng-route實(shí)現(xiàn)基本的路由功能實(shí)例詳解
這篇文章主要介紹了AngularJS通過ng-route實(shí)現(xiàn)基本的路由功能,結(jié)合實(shí)例形式詳細(xì)分析了AngularJS使用ng-route實(shí)現(xiàn)路由功能的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-12-12AngularJS入門(用ng-repeat指令實(shí)現(xiàn)循環(huán)輸出
這篇文章主要介紹了AngularJS入門(用ng-repeat指令實(shí)現(xiàn)循環(huán)輸出,需要的朋友可以參考下2016-05-05