进入子状态时,$ state和$ stateParams未定义

时间:2015-09-23 17:59:27

标签: angularjs angular-ui-router

我面对这样的问题: 我有以下国家层级:预订(摘要) - > services-> new->促销

        $stateProvider.state("bookings", {
        url: "/bookings",
        abstract: true,
        views: {
            "@": {
                templateUrl: "/Static/routing-template.html"
            },
            "content@bookings": {
                templateUrl: "/Static/scheduler/scheduler.html"
            }
        }
    });
        $stateProvider.state("bookings.services", {
        url: "/services",
        views: {
            "menu@bookings": {
                templateUrl: "/Static/services/services-list.html",
                controller: "ServicesController"
            },
            "content@bookings": {
                templateUrl: "/Static/scheduler/scheduler.html"
            }
        },
        resolve: {
            currencies: function(currencyDataService) {
                return currencyDataService.loadData();
            },
            currentCurrency: function(currencyDataService) {
                return currencyDataService.loadCurrent();
            },
            servicesDataService: function($servicesDataService) {
                return $servicesDataService.loadData();
            }

        }
    });
        $stateProvider.state("bookings.services.new", {
        url: "/new/",
        views: {
            "content@bookings": {
                templateUrl: "/Static/services/add-edit-service.html",
                controller: "ServiceAddEditController as controller"
            }
        },
        params: {
            serviceId: 0,
            //to restore state when back from promo setup tab, null when normally add new service 
            serviceModel: null
        }

    });
    $stateProvider.state('bookings.services.new.promo', {
        url: "promo",
        views: {
            "content@bookings": {
                templateUrl: "/Static/services/promo-tab.html",
                controller: "PromoSetupController as promo"
            }
        },
        params: {
            serviceModel: {}
        }
    });

对于前三个步骤,它可以正常工作。但是当我尝试进入promo状态时,控制器初始化但是$ stateParams和$ state是undefined。 PromoControllerCode:

angular.module("CommandCenterApp")
.controller("PromoSetupController", [
    function($state, $stateParams) {
        var self = this;
        self.model = $stateParams.serviceModel;
        console.log("Model:", self.model);
    }
]);

我是如何进入这种状态的:

 <p ui-sref="bookings.services.new.promo({serviceModel:controller.typeModel})" class="fake-link hidden-sm hidden-xs">Set up promo codes</p>

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

它是控制器语法。要么取走括号:

angular.module("CommandCenterApp")
  .controller("PromoSetupController", 
    function($state, $stateParams) {
      var self = this;
      self.model = $stateParams.serviceModel;
      console.log("Model:", self.model);
  }
);

或添加正确的括号语法:

angular.module("CommandCenterApp")
  .controller("PromoSetupController", ['$state', '$stateParams',
    function($state, $stateParams) {
      var self = this;
      self.model = $stateParams.serviceModel;
      console.log("Model:", self.model);
    }
]);