[英]AngularJS routeprovider injection error
I'm learning AngularJS, and I'm now trying the $routeProvider
, but I can't get it to work. 我正在学习AngularJS,我现在正在尝试$routeProvider
,但我无法让它工作。
index.html
: index.html
:
<!DOCTYPE html>
<html ng-app="App">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular-route.min.js"></script>
<script type="text/javascript" src="scripts/controllers.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
view.html
: view.html
:
<p>Hello World!</p>
controllers.js
: controllers.js
:
var app = angular.module('App', ['ngRoute']);
app.config(
function($routeProvider){
$routeProvider.when("/something",{
templateUrl: "view.html",
controller: "MyController"
})
.otherwhise({
redirectTo: "/"
});
}
);
function MyController($scope){
}
Whenever I run this, I get an error message that says 每当我运行它时,我会收到一条错误消息
Uncaught Error: [$injector:modulerr]
How can I solve this? 我怎么解决这个问题?
Change .otherwhise
to .otherwise
. 将.otherwhise
改为.otherwise
。
A good practice when you run into these issues is to try the un-minified version of Angular. 遇到这些问题时的一个好习惯是尝试Angular的非缩小版本。
The un-minified error is much more helpful: 未缩小的错误更有帮助:
Uncaught Error: [$injector:modulerr] Failed to instantiate module App due to: TypeError: Object # has no method 'otherwhise' 未捕获的错误:[$ injector:modulerr]由于以下原因无法实例化模块App:TypeError:Object#没有方法'otherwhise'
Solution: Create a $inject property on your route config as follows: - 解决方案:在路由配置上创建$ inject属性,如下所示: -
var app = angular.module('App', ['ngRoute']);
(function (app) {
var routeConfig = function($routeProvider){
$routeProvider.when("/something",{
templateUrl: "view.html",
controller: "MyController"
})
.otherwhise({
redirectTo: "/"
});
};
routeConfig.$inject = ['$routeProvider'];
app.config(routeConfig);
})(angular.module("App"));
AngularJS will now be able to inject $routeProvider successfully when js is minified. 当js缩小时,AngularJS现在能够成功注入$ routeProvider。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.