Angular $routeParams is blank

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

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

    ...

    And the app (index.html or something)

    ...  

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

提交回复
热议问题