Angular $routeParams is blank

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

    You have a couple of errors:

    1. You've specified the controller in two places, both in the view (<div ng-controller="CompanyCtrl"></div>) and in $routeProvider (.when('/company/edit/:id', {templateUrl: '/partials/edit', controller: 'CompanyCtrl'}). I'd remove the one in the view.

    2. You have to register the controller in the module when specifying it in the $routeProvider (you should really do this anyway, it's better to avoid global controllers). Do napp.controller('CompanyCtrl', function ... instead of var CompanyCtrl = function ....

    3. You need to specify a ng-view when you're using the $route service (not sure if you're doing this or not)

    The new code:

    var napp = angular.module('Napp', ['ngResource']);
    
    napp.controller('CompanyCtrl', function ($scope, $routeParams, $location, $resource) {
      console.log($routeParams);
    });
    
    napp.config(['$routeProvider', function ($routeProvider) {
      $routeProvider
        .when('/company/edit/:id',
          {templateUrl: '/partials/edit', controller: 'CompanyCtrl'}
        );
    }]);
    

    The template (/parials/edit)

    <div> ... </div>
    

    And the app (index.html or something)

    ... <body> <div ng-view></div> </body>
    

    I've created a working plunker example: http://plnkr.co/edit/PQXke2d1IEJfh2BKNE23?p=preview

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