Angular $routeParams is blank

后端 未结 7 1036
不知归路
不知归路 2021-02-19 03:49

I have a really simple Angular app that I\'ve distilled to the following:

var napp = angular.module(\'Napp\',[\'ngResource\']);

var CompanyCtrl = function($scop         


        
相关标签:
7条回答
  • 2021-02-19 04:08

    Of course it will be blank. RouteParams is loaded asynchronously so you need to wait for it to get the params. Put this in your controller:

    $scope.$on('$routeChangeSuccess', function() {
       console.log($routeParams); 
    });
    
    0 讨论(0)
  • 2021-02-19 04:10

    Not sure if this helps, but I just came across this issue myself, and found that I couldn't log the route params until I had something bound to them.
    So,

    Router:

    var myApp = angular.module('myApp', []);
    myApp.config(function($routeProvider){
    
      $routeProvider.when('/projects/:id', 
        {templateUrl: '/views/projects/show.html', controller: 'ProjectCtrl'}
      );
    
    });
    

    Controller:

    myApp.controller('ProjectCtrl', function($scope, $routeParams){
      $scope.id = $routeParams.id;
      console.log('test');
    });
    

    View:

    <h1>{{ id }}</h1>
    

    When I removed the '{{id}}' from the view, nothing was logged and $routeParams was empty, at least at the time of the controller's instantiation. As some of the answers above have pointed to, the route params are passed in asynchronously, so a controller with no bindings to that property won't execute. So, not sure exactly what you've distilled your snippet down from, but hope this helps!

    0 讨论(0)
  • 2021-02-19 04:13

    First of all try this with

    $locationProvider.html5Mode(true);
    

    That should fix your starting code. Then adjust your code to support non-pushState browsers.

    Hope this helps!

    0 讨论(0)
  • 2021-02-19 04:16

    It works for me http://plunker.co/edit/ziLG1cZg8D8cYoiDcWRg?p=preview

    But you have some errors in your code:

    • Your don't seem to have a ngView in your code. The $routeProvider uses the ngView to know where it should insert the template's content. So you need it somewhere in your page.

    • You're specifying your CompanyCtrl in two places. You should specify it either in the $routeProvider, or in you template using ng-controller. I like specifying it in the template, but that's just personal preference.

    • Although not an error, you're specifying your CompanyCtrl in the global scope, instead of registering it on your Napp module using Napp.controller(name, fn).

    Hope this helps!
    You can always go on #angularjs irc channel on freenode: there's always active people ready to help

    0 讨论(0)
  • 2021-02-19 04:28

    Could it be that your templateUrl points to an invalid template?

    When you change the templateUrl to an unexisting file, you will notice that the $routeParams will no longer be populated (because AngularJS detects an error when resolving the template).

    I have created a working plnkr with your code for your convenience that you can just copy and paste to get your application working:

    http://plnkr.co/edit/Yabp4c9zmDGQsUOa2epZ?p=preview

    As soon as you click the link in the example, you will see the router in action.

    Hope that helps!

    0 讨论(0)
  • 2021-02-19 04:29

    This may happen (not in the OP's case) if you're using ui-router instead of ngRoute.

    If that's the case, use $stateParams instead of $routeParams.

    https://stackoverflow.com/a/26946824/995229

    0 讨论(0)
提交回复
热议问题