AngularJS - 如何通过最新的$ routeParams化解? [英] AngularJS - How to pass up to date $routeParams to resolve?

查看:133
本文介绍了AngularJS - 如何通过最新的$ routeParams化解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您传递当前$ routeParams解决,使他们能够被用来做了具体$ http.get怎么办。现在我刚刚测试过$ routeParams并传回在我的控制器将被记录,看看它的工作参数,但每个时间之后的路线。例如,在负载是不确定的,并在后续的航线param是previous路线参数,因此我需要输入一个新的链接,它显示了从previous链接的路线帕拉姆。

How do you pass current $routeParams to resolve so they can be used to do a specific $http.get. Right now I've just tested $routeParams and passed back the param to be logged in my controller to see if it works, but it is a route behind each time. For example, on load it is undefined, and on subsequent routes the param is the previous routes param, so I click a new link and it shows the param from the previous link's route.

路线段

    .when('/something/:paramType', {
        templateUrl: '/app/views/something.html',
        controller: 'SomethingController',
        controllerAs: 'someCtrl',
        access: {
            authentication: true
        },
        resolve: {
            SomeData: function( $routeParams ) {
                return $routeParams.paramType;
            }
        }
    })

控制器段

   .controller('SomethingController', ['$scope', 'SomeData', function( $scope, SomeData) { 

       console.log(SomeData);
   }])

你如何初始化负载帕拉姆,并将其正确同步每一次?我想这样做的原因是这样我就可以执行$ http.get并通过$ routeParams来定义返回的数据。

How do you initialize the param on load, and have it sync correctly each time? The reason I want to do this is so I can perform an $http.get and pass the $routeParams to define the returned data.

推荐答案

角文档为$ routeProvider

From the angular documentation for $routeProvider

请注意,ngRoute $。routeParams仍然会参考previous
  这些解决函数中的路线。使用$ route.current.params到
  访问新路由参数,来代替。

Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. Use $route.current.params to access the new route parameters, instead.

所以..

SomeData: function( $route ) {
    return $route.current.params.paramType;
}

这篇关于AngularJS - 如何通过最新的$ routeParams化解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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