是ngRouter的角度错误还是我做错了什么

Is it an angular ngRouter bug or am i doing something wrong

本文关键字:错了 什么 错误 ngRouter      更新时间:2024-01-12

此代码来自ngRouter 的AngularJs文档

angular.module('ngViewExample', ['ngRoute', 'ngAnimate'],
  function($routeProvider, $locationProvider) {
    $routeProvider.when('/Book/:bookId', {
      templateUrl: 'book.html',
      controller: BookCntl,
      controllerAs: 'book'
    });
    $routeProvider.when('/Book/:bookId/ch/:chapterId', {
      templateUrl: 'chapter.html',
      controller: ChapterCntl,
      controllerAs: 'chapter'
    });
    // configure html5 to get links working on jsfiddle
    $locationProvider.html5Mode(true);
});
function MainCntl($route, $routeParams, $location) {
  this.$route = $route;
  this.$location = $location;
  this.$routeParams = $routeParams;
}
function BookCntl($routeParams) {
  this.name = "BookCntl";
  this.params = $routeParams;
}
function ChapterCntl($routeParams) {
  this.name = "ChapterCntl";
  this.params = $routeParams;
}

让我们集中关注出现错误的元素:

BookCtrl:

function BookCntl($routeParams) {
  this.name = "BookCntl";
  this.params = $routeParams;
}

章节Ctrl:

function ChapterCntl($routeParams) {
  this.name = "ChapterCntl";
  this.params = $routeParams;
}

路由器功能

  function($routeProvider, $locationProvider) {
    $routeProvider.when('/Book/:bookId', {
      templateUrl: 'book.html',
      controller: BookCntl,
      controllerAs: 'book'
    });
    $routeProvider.when('/Book/:bookId/ch/:chapterId', {
      templateUrl: 'chapter.html',
      controller: ChapterCntl,
      controllerAs: 'chapter'
    });
    // configure html5 to get links working on jsfiddle
    $locationProvider.html5Mode(true);
});

现在,如果你在plunker上进行编辑,这将正确工作
但是,如果我们像这样以不同的方式声明BookCtrl和ChapterCtrl:

angular.module('ngViewExample', ['ngRoute', 'ngAnimate'],
  function($routeProvider, $locationProvider) {
    $routeProvider.when('/Book/:bookId', {
      templateUrl: 'book.html',
      controller: BookCntl,
      controllerAs: 'book'
    });
    $routeProvider.when('/Book/:bookId/ch/:chapterId', {
      templateUrl: 'chapter.html',
      controller: ChapterCntl,
      controllerAs: 'chapter'
    });
    // configure html5 to get links working on jsfiddle
    $locationProvider.html5Mode(true);
}).controller('MainCntl', function($route, $routeParams, $location) {
  this.$route = $route;
  this.$location = $location;
  this.$routeParams = $routeParams;
}).controller('BookCntl', function($routeParams) {
  this.name = "BookCntl";
  this.params = $routeParams;
}).controller('ChapterCntl', function($routeParams) {
  this.name = "ChapterCntl";
  this.params = $routeParams;
});

主ngViewExample模块不再定义,因为它找不到BookCtrl,导致此错误:

未能实例化模块ngViewExample,原因是:ReferenceError:BookCntl未定义在http://run.plnkr.co/WS5cdGZ2VT2oHDLR/script.js:5:19在Object.d[作为调用](http://code.angularjs.org/1.2.3/angular.min.js:30:232)在http://code.angularjs.org/1.2.3/angular.min.js:29:58在Array.forEach(本地)在q(http://code.angularjs.org/1.2.3/angular.min.js:7:255)在e(http://code.angularjs.org/1.2.3/angular.min.js:28:374)在Yb(http://code.angularjs.org/1.2.3/angular.min.js:32:427)在Xb.c(http://code.angularjs.org/1.2.3/angular.min.js:17:315)在Xb(http://code.angularjs.org/1.2.3/angular.min.js:18:30)在Rc(http://code.angularjs.org/1.2.3/angular.min.js:17:99

所以基本上ngViewExample没有被实例化,因为BookCntl没有被定义
如何告诉路由器功能在模块的控制器中查找BookCntl
或者我应该将此报告为angular站点中的一个错误。

如果这样声明,则必须在$routeProvider定义中的控制器名称上使用引号。

angular.module('ngViewExample', ['ngRoute', 'ngAnimate'],
  function($routeProvider, $locationProvider) {
    $routeProvider.when('/Book/:bookId', {
      templateUrl: 'book.html',
      controller: 'BookCntl',
      controllerAs: 'book'
    });
    $routeProvider.when('/Book/:bookId/ch/:chapterId', {
      templateUrl: 'chapter.html',
      controller: 'ChapterCntl',
      controllerAs: 'chapter'
    });
    // configure html5 to get links working on jsfiddle
    $locationProvider.html5Mode(true);
}).controller('MainCntl', function($route, $routeParams, $location) {
  this.$route = $route;
  this.$location = $location;
  this.$routeParams = $routeParams;
}).controller('BookCntl', function($routeParams) {
  this.name = "BookCntl";
  this.params = $routeParams;
}).controller('ChapterCntl', function($routeParams) {
  this.name = "ChapterCntl";
  this.params = $routeParams;
});

这应该行得通。