Resolve mechanism for routing (need to understand promise)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.