Ui Router Resolve - 之后取消状态控制器加载?

时间:2014-10-04 16:21:18

标签: angularjs angular-ui-router

Angular UI路由器问题

$state.go("main.loadbalancer.readonly");块中激活resolve后,main.loadbalancer.vips状态控制器VipListCtrl(仅限控制器)仍然在解析后加载。

由于状态main.loadbalancer.readonly已激活,如何让控制器VipListCtrl取消而不加载?

我尝试使用承诺并且永远不会解除承诺,但是UI路由器似乎永远保持着这种决心。

angular.module("main.loadbalancer", ["ui.bootstrap", "ui.router"]).config(function($stateProvider) {
  return $stateProvider.state("main.loadbalancer", {
    url: "device/:id",
    views: {
      "content@": {
        templateUrl: "loadbalancer/loadbalancer.html",
        controller: "LoadBalancerCtrl"
      }
    }
  }).state("main.loadbalancer.vips", {
    resolve: {
        isDeviceReadOnly: function($state) {
        if (!$state.current.data['deviceId']) {
          $state.go("main.loadbalancer.readonly"); //THIS IS RAN...NEED CONTROLLER
                                                   //VipListCtrl TO NOT RUN AFTERWARDS
        }
      }
    },
    url: "/vips",
    templateUrl: "loadbalancer/vip-table.html",
    controller: "VipListCtrl"
  }).state("main.loadbalancer.nodes", {
    url: "/nodes",
    templateUrl: "loadbalancer/node-table.html",
    controller: "NodeListCtrl"
  }).state("main.loadbalancer.admin", {
    url: "/admin",
    templateUrl: "loadbalancer/admin.html",
    controller: "AdminCtrl"
  }).state("main.loadbalancer.readonly", {
    url: "/readonly",
    templateUrl: "loadbalancer/readonly.html",
    controller: "ReadonlyCtrl"
  });
});

1 个答案:

答案 0 :(得分:6)

 resolve: {
        isDeviceReadOnly: function($state, $q, $timeout) {
        if (!$state.current.data['deviceId']) {
          $timeout(function() { $state.go("main.loadbalancer.readonly"); });
          return $q.reject("rejection message"); // <-- Gotta reject your resolve
        }
      }
    },

plunk,演示$ q.reject取消转换:http://plnkr.co/edit/njJtyVbKD4rDY3OckAF6?p=preview