简体   繁体   English

Rails路由参数成角度

[英]Rails route params in angular

I'm playing around with angular in my rails app. 我在Rails应用程序中玩着angular。 This is not a single page app, i'm just using angular in a few places. 这不是一个单页面的应用程序,我只是在一些地方使用了Angular。 I'm having a hard time getting route params from the uri to use in my resources. 我很难从uri获取路线参数以在我的资源中使用。

For instance, say I'm on the page /users/1/posts/2. 例如,说我在/ users / 1 / posts / 2页面上。 How do I get both user_id and id(for post)? 如何同时获取user_id和id(用于发布)?

Thanks. 谢谢。

You could configure your angular app to pass the params to the controller your using for that page. 您可以配置角度应用程序以将参数传递给该页面使用的控制器。

I would make a routes.js file doing something like: 我将做一个routes.js文件,例如:

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
      }]
    }
  })
}])

Then in your controller - "yourNGController' inject 'params' that you resolved in your route. That should get your the info you need into the controller. 然后在您的控制器中-“ yourNGController”注入您在路径中解析的“ params”,这应该将所需的信息输入到控制器中。

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. 如果使用较新版本的angular,您可能还需要包含angular-route.js,因为$ routeProvider已从核心角度组件中删除。

我最终只使用了ui-routes,这使这个问题变得容易得多。

I had a similair issue, and it is linked here: Passing Rails ID to Angular 我有一个类似的问题,它在这里链接:将Rails ID传递给Angular

I consider this solution a bit of a hack, so am interested in better approaches. 我认为此解决方案有点麻烦,因此对更好的方法感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM