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)用,感興趣的朋友們可以一起來(lái)學(xué)習(xí)學(xué)習(xí)。
一、瀏覽器判斷
在angular做微信應(yīng)用的時(shí)候,有時(shí)候我們也想把相同一份代碼運(yùn)行在非微信的瀏覽器上,這時(shí)候我們可以在angular的run上寫(xiě)點(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(" 是來(lái)自微信內(nèi)置瀏覽器");
return true;
} else {
console.log("不是來(lái)自微信內(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里面寫(xiě)登陸判斷是一種不錯(cuò)的方案,例如下面我寫(xiě)的這段,配合cookie和我上面的瀏覽器判斷,當(dāng)我加載頁(yè)面的時(shí)候我就可以調(diào)用$rootScope.goLogin方案來(lái)判斷是否這個(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)寫(xiě)過(guò)一個(gè)這樣的函數(shù)來(lái)實(shí)現(xiàn)路由的參數(shù)判斷,來(lái)設(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來(lái)決定進(jìn)入那個(gè)parts
window.cats = getParam('AutumnsWind');
后來(lái)改進(jìn)了下面這個(gè)簡(jiǎn)單的例子,就可以不用用上面那句代碼來(lái)實(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)用一下正則,這樣就能很好的過(guò)濾我們禁止的url,由于例子就不寫(xiě)這么復(fù)雜啦~
四、設(shè)置公共參數(shù)
這個(gè)其實(shí)就不用寫(xiě)例子了,因?yàn)樯厦娴睦右菜闶沁@個(gè)的一部分吧~
總結(jié)
以上就是關(guān)于Angular中run方法巧妙運(yùn)用的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- angularJS中$apply()方法詳解
- 使用AngularJS來(lái)實(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)文章
詳解AngularJS中module模塊的導(dǎo)入導(dǎo)出
本文給大家介紹angularjs中module模塊的導(dǎo)入導(dǎo)出,涉及到angularjs module相關(guān)知識(shí),對(duì)angularjs module感興趣的朋友一起看看吧2015-12-12
AngularJs ng-route路由詳解及實(shí)例代碼
這篇文章主要介紹了AngularJs ng-route路由,這里整理相關(guān)資料及簡(jiǎn)單實(shí)例代碼,有興趣的小伙伴可以參考下2016-09-09
詳解Angular.js數(shù)據(jù)綁定時(shí)自動(dòng)轉(zhuǎn)義html標(biāo)簽及內(nèi)容
本篇文章主要介紹了詳解Angular.js數(shù)據(jù)綁定時(shí)自動(dòng)轉(zhuǎn)義html標(biāo)簽及內(nèi)容 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Angular 開(kāi)發(fā)學(xué)習(xí)之Angular CLI的安裝使用
這篇文章主要介紹了Angular 開(kāi)發(fā)學(xué)習(xí)之Angular CLI的安裝使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
AngularJS報(bào)錯(cuò)$apply already in progress的解決方法分析
這篇文章主要介紹了AngularJS報(bào)錯(cuò)$apply already in progress的解決方法,較為詳細(xì)的分析了報(bào)錯(cuò)$apply already in progress的原理、處理技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-01-01
簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用
這篇文章主要介紹了簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用的相關(guān)資料,通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),需要的朋友可以參考下2022-08-08

