是ngRouter的角度错误还是我做错了什么
Is it an angular ngRouter bug or am i doing something wrong
此代码来自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;
});
这应该行得通。
- 正在尝试为Docpad网站设置Lunr全文搜索插件.我做错了什么
- 重新加载重复数据失败,我做错了什么
- 我做错了什么
- Google Apps 脚本为 getLastRow 抛出电子邮件失败通知,我做错了什么
- Javascript语法 - 我做错了什么
- 角度灯箱不起作用.我做错了什么
- 在这个猫鼬独特的保存前验证中,我做错了什么
- 简单的Youtube iframe API就在文档中,我做错了什么
- 我正在尝试实现一个AJAX调用.我做错了什么
- 我对这个动态查询做错了什么
- 我在jQuery事件处理和隐藏/显示图像元素方面做错了什么
- 闭包中的本地变量从外部更改.这是一个javascript错误,或者我做错了什么
- JS离线检测?我做错了什么
- Jquery自动完成不是't工作.我做错了什么
- 是ngRouter的角度错误还是我做错了什么
- jQuery Toggle——我做错了什么
- javascript原型,我做错了什么
- 超级初学者JavaScript..我做错了什么
- 我在这里做错了什么?否则,如果
- Javascript和HTML - 添加一个下拉菜单,我在这里做错了什么