【问题标题】:ui-router pass URL query parameters to controller via service - $stateParams values 'undefined'ui-router 通过服务将 URL 查询参数传递给控制器​​ - $stateParams 值“未定义”
【发布时间】:2016-10-01 07:51:22
【问题描述】:

使用 ui-router 并尝试将 URL 查询参数传递给模块的 .config 中的控制器,使用已注入到 .state 定义的 resolve 参数中的 .service

首选方法 - (不可靠)

.state('list', {
    url: '/list?country&state&city',
    templateUrl: "list.html",
    controller : 'myController',
    resolve: {
          currentUrlTypeParam : ['myServices', function(myServices){        // object variable that can be injected into specified controller
              return myServices.getUrlTypeParams();
            }]
        }
  })

虽然文档说只有 Constant 和 Provider 配方可以注入到模块的 .config 函数中,但我遵循 .state 定义的 this method of injecting a .service into the resolve argument

我使用服务的原因是我有许多具有相同 URL 查询参数的状态 - 所以作为服务提供者编写一次函数而不是重复它是有意义的。

我使用的是resolve,而不是直接将$stateParams 注入控制器,这样我就可以在配置期间完成服务器调用。

Here's a plunker 这个(显示 3 种可能的具有相同 URL 参数的状态) - 但是它不能可靠地工作 - 正确的 URL 参数似乎在第二次点击时到达相同状态的不同 URL。

更令人沮丧的是,虽然$stateParams 似乎在.service 函数中正确到达(在第一次点击时) - 但是在尝试访问它们时值出现为undefined - 检查plunker @987654338 @看到这个奇怪的现象!

例如
console.log将在.service函数中输出$stateParams为:
{country: "US", state: undefined, city: undefined}
但是,当我在函数中调用$stateParams.country 时,我得到undefined ??

替代梅西耶方法 - (工作正常)

替代方法是在 每个状态定义resolve 参数中重复相同的大 .service 代码 - 这可以可靠地工作,但会使代码膨胀(...这是一个简化的示例我正在处理的状态/参数)。

.state('list', {
    url: '/list?country&state&city',
    templateUrl: "list.html",
    controller : 'myController',
    resolve: {
        currentUrlTypeParam : ['$stateParams', function($stateParams){
            var urlTypeParameter = {}                                                                               // define an object that we'll return as the value for $scope.loadMethodParameters
            if($stateParams.country && typeof $stateParams.country !== undefined){
                urlTypeParameter.type = 'country';
                urlTypeParameter.parameter = $stateParams.country;
            } else if($stateParams.state && typeof $stateParams.state !== undefined){
                urlTypeParameter.type = 'state';
                urlTypeParameter.parameter = $stateParams.state;
            } else if($stateParams.city && typeof $stateParams.city !== undefined){
                urlTypeParameter.type = 'city';
                urlTypeParameter.parameter = $stateParams.city;
            } else {
                urlTypeParameter.type = 'default';
            }
            return urlTypeParameter;
        }]
    }
})

请参阅this plunker 了解实现相同目标的一种相当……不优雅的方式。

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    你的服务不工作的原因是因为你试图注入$stateParams,而不是把它传递给服务函数本身。

    与其将$stateParams 注入您的服务并运行return myServices.getUrlTypeParams();,不如尝试将$stateParams 传递给您的服务函数。例如:

    resolve: {
        currentUrlTypeParam : ['$stateParams', 'myServices', function($stateParams, myServices){        // object variable that can be injected into specified controller
            return myServices.getUrlTypeParams($stateParams);
        }]
    }
    

    然后你必须修改你的服务以注入$stateParams,而是将它传递给你的服务函数:

    this.getUrlTypeParams = function($stateParams){ ... }
    

    【讨论】:

    • 谢谢,我会试一试 - 使用我的“首选”方法 $stateParams 如何完美地到达服务中,但无法访问其值?
    • 这可能是因为 when $stateParams 更新。无论如何,虽然 $stateParams 可以被认为是整个应用程序中的可注入模块,但它实际上应该只注入到控制器或解析中,而不是其他任何地方。
    • 实际上,我刚刚意识到这是因为您正在记录 $stateParams 对象。每当您调用console.log() 时,您看到的不是日志时的对象,而是在控制台中查看对象时。因此,这些值似乎存在并且已经更新,而实际上它们没有。您可以通过记录整个 params 对象来查看这一点,只需记录其中一个孩子,例如 console.log($stateParams.country)
    • 是的,你是对的 - stackoverflow.com/a/18565594/3092596 - 如果你想在你的答案中包含它,我已经更新了原来的 plunker:plnkr.co/edit/ZnERL1OsopHopXiLXJC5?p=preview
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多