javascript AngularJS - 如何传递最新的 $routeParams 来解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25878850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS - How to pass up to date $routeParams to resolve?
提问by mtpultz
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.
您如何通过当前的 $routeParams 来解析,以便它们可以用于执行特定的 $http.get。现在我刚刚测试了 $routeParams 并将参数传回以登录到我的控制器中以查看它是否有效,但它每次都是后面的路由。例如,在加载时它是未定义的,并且在后续路由中,参数是之前的路由参数,所以我点击了一个新链接,它显示了来自前一个链接的路由的参数。
Route Snippet
路由片段
.when('/something/:paramType', {
templateUrl: '/app/views/something.html',
controller: 'SomethingController',
controllerAs: 'someCtrl',
access: {
authentication: true
},
resolve: {
SomeData: function( $routeParams ) {
return $routeParams.paramType;
}
}
})
Controller Snippet
控制器代码段
.controller('SomethingController', ['$scope', 'SomeData', function( $scope, SomeData) {
console.log(SomeData);
}])
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.
你如何在加载时初始化参数,并让它每次都正确同步?我想这样做的原因是我可以执行 $http.get 并传递 $routeParams 来定义返回的数据。
回答by David Beech
From the angular documentationfor $routeProvider
来自$routeProvider的角度文档
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.
请注意 ngRoute.$routeParams 仍将引用这些解析函数中的先前路由。改为使用 $route.current.params 来访问新的路由参数。
So..
所以..
SomeData: function( $route ) {
return $route.current.params.paramType;
}