UI-Router解析函数只调用一次 - 即使使用重载选项也是如此

时间:2014-10-04 06:03:08

标签: angularjs angular-ui-router

在Angular ui路由器中,在$state.go("main.loadbalancer.readonly");先前被激活后运行main.loadbalancer.readonly时,我的决心:{}未被唤醒/执行。简单地绕过了解决方案。我已经通过console.log($state.current.data['deviceId']);未显示来验证这一点。

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) {
          console.log($state.current.data['deviceId']);
          if (!$state.current.data['deviceId']) {
            console.log("pimp");
            $state.go("main.loadbalancer.readonly");
        } 
      } 
    },
    url: "/vips",
    templateUrl: "loadbalancer/vip-table.html",
    controller: "VipListCtrl"
  }).state("main.loadbalancer.readonly", {
    url: "/readonly",
    templateUrl: "loadbalancer/readonly.html",
    controller: "ReadonlyCtrl"
  });
});

控制器代码:

  submit = function() {
    $state.current.data = { deviceId: false };
    return LoadBalancerSvc.searchDevice($scope.searchInput.value).get().then(function(lb) {
      console.log(lb.ha_status);
      if (lb.ha_status == "secondary") {
        console.log("hi");
        $state.current.data['deviceId'] = false;
        $state.go("main.loadbalancer.readonly"); //WHEN THIS IS RAN A SECOND TIME
                                                   AFTER STATE HAS BEEN ACTIVE BEFORE
        $state.deviceReadonly = true
      } else {
        $state.current.data['deviceId'] = lb.id;
        $state.deviceReadonly = false;
        SearchSvc.updateDeviceNumber(lb.id);
        $state.go("main.loadbalancer.vips", {id: lb.id});
        console.log("bye");
      }
    });
  };

我只能猜测,因为main.loadbalancer.vips之前已被激活,然后到ui路由器,这意味着一旦解决了总是已解决。如何评估每次激活状态时$state.go("main.loadbalancer.readonly")解析?

注意:我也尝试$state.go("main.loadbalancer.readonly", { reload: true });没有成功。

1 个答案:

答案 0 :(得分:1)

似乎'transitionTo'起作用了。

 if (lb.ha_status == "secondary") {
        $state.current.data['deviceId'] = false;
        $state.transitionTo("main.loadbalancer.readonly", {}, { reload: true });
        $state.deviceReadonly = true
      }