Javascript AngularJS routeprovider注入错误
我正在学习AngularJS,现在我正在尝试Javascript AngularJS routeprovider注入错误,javascript,html,angularjs,Javascript,Html,Angularjs,我正在学习AngularJS,现在我正在尝试$routeProvider,但我无法让它工作 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"></sc
$routeProvider
,但我无法让它工作
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>
<p>Hello World!</p>
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){
}
每当我运行此程序时,都会收到一条错误消息
Uncaught Error: [$injector:modulerr]
如何解决此问题?将
更改为。否则将更改为
遇到这些问题时,一个好的做法是尝试Angular的未缩小版本
未缩小的错误更有帮助:
未捕获错误:[$injector:modulerr]未能实例化模块应用程序
由于:TypeError:Object#没有方法“otherwhise”
解决方案:在路由配置上创建$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现在将能够在js缩小后成功注入$routeProvider。它不应该是app.config(['$routeProvider'],function($routeProvider){
我真蠢!感谢关于非缩小版本的提示,缩小版本的错误非常神秘。遇到这些问题时,一个好的做法是尝试Angular的未缩小版本。未缩小的错误更有用:“-这帮助我解决了我的问题。(y)