search

Home  >  Q&A  >  body text

javascript - angular 中的$routeParams和$route.current.params的区别是什么?

路由:

.when('/view/:recipeId',{
        controller:'ViewRecipe',
        templateUrl:'views/viewRecipe.html',
        resolve:{
            recipe:['loadRecipe',function(loadRecipe){
                 return loadRecipe()
            }]
        }
    })

当路由跳到 http://10.22.7.40:9000/#/view/3 的时候
使用$route.current.params为{recipeId:3},但是$routeParams却是{}
想知道原因是什么? 谢谢~

阿神阿神2902 days ago458

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-10 14:32:25

    我不知道你是在哪里查看 $routeParams 的,我猜是在 resolve: 那里吧?这就是原因。请仔细阅读 $routeParams 的文档,不长,10 分钟就读完了。帮你总结一句话:

    Note that the $routeParams are only updated after a route change completes successfully. This means that you cannot rely on $routeParams being correct in route resolve functions.

    然后你可以再看看文档里的这个例子,注意这个例子里模拟了异步的延迟,于是当你切换 routes 的时候,仔细观察 $route.current.params$routeParams 的变化顺序;另外看清楚这个例子是在哪里去获取 $routeParams 的。

    要学会看文档呀~

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 14:32:25

    建议使用angular-ui-router插件,获取参数直接通过$stateParams即可

    reply
    0
  • Cancelreply