routeProvider angularJS 1.3.x错误:$ injector:modulerr

时间:2015-06-06 11:51:55

标签: angularjs

我对angularjs有点问题,这个问题有很多答案,但我无法解决这个问题。

这是我在html中的导入

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-resource.min.js"></script>

App.js

angular.module('ngApp',['ngRoute'])
.config(['$routeProvider'], 
    function($routeProvider){ // Error here !

}).controller('ngAppHome',function ($scope){
  $scope.title = ""; 
});

错误(在chrome中)

Failed to instantiate module ngApp due to:
Error: [ng:areq] http://errors.angularjs.org/1.3.16/ng/areq?p0=fn&p1=not%2...
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:6:417
    at Sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:19:510)
    at La (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:20:78)
    at Function.bb.$$annotate (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:175:178)
    at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:36:147)
    at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:34:498)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:35:117
    at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:7:322)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:34:399

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

config()需要一个参数,而不是2.要么它是回调函数本身,要么是包含提供程序名称的数组,后跟回调函数:

angular.module('ngApp',['ngRoute']).config(function($routeProvider){ 
    ...
})

angular.module('ngApp',['ngRoute']).config(['$routeProvider', function($routeProvider) { 
    ...
}])

鉴于您的代码无论如何都无法抵抗缩小,因为您不使用控制器的数组语法,我会使用第一个简单的。在构建中使用ngAnnotate可以使代码可以缩小。