在轨角航线PARAMS [英] Rails route params in angular
问题描述
我与角玩弄我的rails应用程序。这不是一个单页的应用程序,我只是在一些地方采用了棱角分明。我有一个很难从URI获得途径PARAMS在我的资源使用。
I'm playing around with angular in my rails app. This is not a single page app, i'm just using angular in a few places. I'm having a hard time getting route params from the uri to use in my resources.
例如,假设我在页面上/用户/ 1 /职位/ 2。我如何获得这两个USER_ID及标识(职位)?
For instance, say I'm on the page /users/1/posts/2. How do I get both user_id and id(for post)?
感谢。
推荐答案
您可以配置您的角度应用到PARAMS传递到您的使用该页面的控制器。
You could configure your angular app to pass the params to the controller your using for that page.
我会做一个routes.js文件做这样的事情:
I would make a routes.js file doing something like:
App.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/your-route', {
controller: 'yourNGController',
resolve: {
params: ['$route', function($route){
params = {user_id: $route.current.params.user_id}
params.merge({post_id: $route.current.params.post_id})
return params
}]
}
})
}])
然后在你的控制器 - yourNGController'注入'参数',你在你的路线决定,应该让你的信息,你需要在控制器
Then in your controller - "yourNGController' inject 'params' that you resolved in your route. That should get your the info you need into the controller.
如果使用的角度较新版本,你可能还需要包括角route.js为$ routeProvider从核心角度成分去除。
If using newer versions of angular you may also need to include the angular-route.js as $routeProvider was removed from the core angular components.
这篇关于在轨角航线PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!