Verifying route preconditions prior to loading controller

前端 未结 4 707
广开言路
广开言路 2021-02-15 17:27

I\'m writing a single page application in Angular, specifically angular.dart, but I\'m assuming this question still applies to AngularJS.

Take for example the followin

4条回答
  •  无人及你
    2021-02-15 18:12

    Note: this applies to AngularJS only, leaving this up in case someone using JS needs it.

    Take a look at the resolve property in $routeProvider. You can return a promise which will determine whether the route change should be accepted or not.

    From the docs:

    If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. If any of the promises are rejected the $routeChangeError event is fired.

    There's also a video about it and the route lifecycle on egghead.io here

提交回复
热议问题