The resovle mechanism of angular is actually applied promise, giving us a chance to do pre-processing before entering a specific route.
1. Before entering this route, lazy load the corresponding. js
1 $stateProvider2. State (' Owner_detail_room ',{3URL: '/owner_detail_room/{id:[0-9]{1,10}} ',4 views:{5' Main ' : {6Templateurl:function() {return' Templates/owner_detail_room.html '},7Controller: ' Owner_detail_roomctrl '8 }9 },Ten resolve:{ OneLoadctrl: [' $ocLazyLoad ',function($ocLazyLoad) { A return$ocLazyLoad. Load (' Js/owner_detail_roomctrl.js ') - }] - } the})
2. Note that resolve is an object whose key is specified by us, and value is a promise asynchronous request or string
First, an example of multiple key-value pairs 1-lazy loading js,2-Get user,3-Judge page permissions
1. State (' Owner_detail_room ',{2URL: '/owner_detail_room/{id:[0-9]{1,10}} ',3 views:{4' Main ' : {5Templateurl:function() {return' Templates/owner_detail_room.html '},6Controller: ' Owner_detail_roomctrl '7 }8 },9 resolve:{TenLoadctrl: [' $ocLazyLoad ',function($ocLazyLoad) { One return$ocLazyLoad. Load (' Js/owner_detail_roomctrl.js ') A }], -User: [' userservice ',function(userservice) { - returnUserservice.getuser (); the }], -userlevelcheck:[' LevelCheck ', ' UserService ',function(levelcheck,userservice) { - varIspass =Levelcheck.check (userservice.userlevel); - if(ispass) { + returnUserservice.userlevel; -}Else{ +$state. Go (' Level-is-not-enough '); A } at }] - } -})
Then in the corresponding page of the CTRL, you can inject resolve in the return
1 function ($scope, users) {2 $scope. Users = Users ; 3 });
3, from resolve to enter the route CTRL, applies to the Promise, ($q service Properties Method: $q. Defer (), $q. All (), $q. When (), $q. Reject (), $q. Resolve (). )
A chestnut, the rest of us,
1 Resolve: {2Datafunction(User, Post, $q) {3Deferred =$q. Defer ();4 5$q. All ([User.getall, Post.gettops ()]). Then (function(results) {6 Deferred.resolve ({7Users:results[0],8Posts:results[1]9 })Ten }); One A returndeferred.promise; - } -}
Finally, if there are errors, be sure to point out
Resolve mechanism for routing (need to understand promise)