Angular $routeParams is blank

后端 未结 7 1009
不知归路
不知归路 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: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:

    {{ id }}

    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!

提交回复
热议问题