0

I need to pass id from project/id to CommentCtrl, when I try to console.log($routeParams.id) it return undefined... Anyone can find what wrong with my code?

   .config(function($routeProvider) {
                    $routeProvider.
                            when('/project/:id', {
                                controller: 'CommentsCtrl'
                            }).
                            otherwise({
                                redirectTo: '/'
                            });
                })
              .controller('CommentsCtrl',function($scope,$http,$routeParams){
                  $scope.comments = [];
                  $scope.param = $routeParams.id;
              });

Update:

In html I just have module , CommentsCtrl and <div ng-view></div> inside CommentsCtrl like this:

<div ng-controller="CommentsCtrl">
  <div ng-view></div>
</div>
8
  • What's the result if you console.log($routeParams) ? Commented Aug 6, 2015 at 14:46
  • Please add you html structure for more info, including ng-view Commented Aug 6, 2015 at 14:48
  • I was quicker :) - Remove ng-controller Commented Aug 6, 2015 at 14:52
  • Why to remove ng-controller ? Commented Aug 6, 2015 at 14:53
  • ngRoute assigns the Controller controller: 'CommentsCtrl', you're doubling it Commented Aug 6, 2015 at 14:53

1 Answer 1

1

I've put my comments into an answer for better explanation.

You have to adjust your html structure to look like this:

<div>
  <div ng-view></div>
</div>

Remove ng-controller, because ngRoute assigns the controller, as you can see in your config:

[...]
  $routeProvider.
    when('/project/:id', {
        controller: 'CommentsCtrl'
    })

Then CommentsCtrl will get $routeParams.id as a parameter.

If you should need a wrapping controller, you'll have to define a different one, like 'AppCtrl' or something. But it must not match a controller used with ngRoute.


This is the simplest example I could assemble for how to use ngRoute: http://plnkr.co/edit/JSwafRF2AavvlWtsnjus?p=preview

You can test it in the fullscreen view, by modifying the url. I couldn't get the links running on plnkr up to now

Update:

There's some things to mention, considering my example code:

  • With plunker, I have to use the hashes (#), because to server doesn't allow html5 style links. This might be different for your app so you might leave out the hashes.

  • I've removed the project path in my route config for simplicity

If this doesn't help at all, it might help if you provide a live view of your app, so I can verify specifically what doesn't work or whats wrong.

Sign up to request clarification or add additional context in comments.

3 Comments

I implement this in my code, but problem is I always get Here we have no params also I don't know why on my url is added # and looks like this: /project/3#/
Please see my plunk again, I've just added links and a wrapping controller. Maybe helps
I found solution here: scotch.io/quick-tips/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.