UI路由器解决与动态参数 [英] ui-router resolve with dynamic parameters

查看:111
本文介绍了UI路由器解决与动态参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是简单,但我无法找到的文档任何东西,google搜索并没有帮助。我想在$ stateProvider定义的状态,我需要打服务器上的URL来拉所需要的数据取决于一个国家的URL参数。总之,是这样的:

This is probably simple but I can't find anything in the docs and googling didn't help. I'm trying to define a state in $stateProvider where the url I need to hit on the server to pull the needed data depends on a state url parameter. In short, something like:

 .state('recipes.category', {
    url: '/:cat',
    templateUrl: '/partials/recipes.category.html',
    controller: 'RecipesCategoryCtrl',
    resolve: {
      category: function($http) {
        return $http.get('/recipes/' + cat)
          .then(function(data) { return data.data; });
      }
    }
  })

以上不起作用。我试图注入$ routeParams得到所需要的'猫'参数,没有运气。什么是这样做的正确方法吗?

The above doesn't work. I tried injecting $routeParams to get the needed 'cat' parameter, with no luck. What's the right way of doing this?

推荐答案

您已经接近与 $ routeParams 。如果您使用的UI路由器使用 $ stateParams 来代替。
这code为我工作:

You were close with $routeParams. If you use ui-router use $stateParams instead. This code works for me:

.state('recipes.category', {
    url: '/:cat',
    templateUrl: '/partials/recipes.category.html',
    controller: 'RecipesCategoryCtrl',
    resolve: {
         category: ['$http','$stateParams', function($http, $stateParams) {
             return $http.get('/recipes/' + $stateParams.cat)
                    .then(function(data) { return data.data; });
         }]
     }
})

这篇关于UI路由器解决与动态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆