Preface
AngularJS is maintained by Google. It is already very popular abroad, but there is a big gap in usage in China, and references/online articles are also scarce. There are few introductions to the run method in AngularJS online. This article mainly summarizes the clever use of the run method in AngularJS. Interested friends can learn together.
1. Browser judgment
When angular is doing WeChat applications, sometimes we also want to run the same code on non-WeChat browsers. At this time, we can write something on angular run to implement it~
For exampleExecute a function to define a
$
Functions of
.run(['$rootScope', '$route', '$window', '$location', 'Position', '$cookies', 'Request', '$cookieStore', function($rootScope, $route, $window, $location, position, $cookies, request, $cookieStore) { //Login without WeChat $ = function() { //Document whether to log in via WeChat var ua = (); //(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 ((/MicroMessenger/i) == 'micromessenger') { ("It's from the built-in browser of WeChat"); return true; } else { ("Not from the built-in browser of WeChat"); return false; } }; ]);
This way it can be executed in advance before other parts of the application, and then according to$
We can effectively determine whether it is a WeChat browser in different views or controllers.
('autumnswind').controller('OrderCtrl', ['$rootScope', '$scope', 'Request', '$cookies', '$window', '$routeParams', '$location', 'Tool', function($rootScope, $scope, request, $cookies, $window, $routeParams, $location, tool) { if ($()) { ... } } ]);
2. Login judgment
Writing login judgment in run is a good solution. For example, the following paragraph I wrote, in conjunction with cookies and the browser judgment above, I can call it when I load the page$
The solution is to determine whether the view where the route is located is login. If there is this legal cookie, let it continue to run, otherwise return to the login page to login ~
$ = function(replace) { if ($()) { if (!replace) { $('loginBack'); delete $; $('login'); } else { $ = $(); $('login').replace(); } } else { $('loginBack'); delete $; $('loginWebapp'); } };
3. Whitelist settings
I once wrote a function like this to determine the parameter of the route and set a whitelist. At that time, this function was still placed in the global variable. In fact, it is not a good method to think about it.
var getParam = function(name) { var search = ; var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g"); var matcher = (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; }; //This is determined based on the route name to enter which parts to enter = getParam('AutumnsWind');
Later, the following simple example was improved, so you can do it without using the above code.
$rootScope.$on('$routeChangeSuccess', function() { var route = ; if (('/hello/') != -1 && ('/autumnswind/') != -1) { = ; } else if (('#/index') != -1) { = ; } else if (('#/asw'scat/') != -1) { = ; } else { //Jump the download page = '~autumns~.cn'; } );
Above we set the whitelist according to the changes in the route. If it is more complicated, you can use the regularity, so that we can filter the urls we prohibited well. Since the examples are not written so complicated~
4. Set public parameters
Actually, there is no need to write an example for this, because the above example is also part of this~
Summarize
The above is all about the clever use of the run method in Angular. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.