ui-router 使用动态参数解析 [英] ui-router resolve with dynamic parameters

查看:37
本文介绍了ui-router 使用动态参数解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很简单,但我在文档中找不到任何内容,谷歌搜索也无济于事.我试图在 $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 以获取所需的 cat 参数,但没有成功.这样做的正确方法是什么?

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-router,请改用 $stateParams.此代码适用于我:

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-router 使用动态参数解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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