AngularJS上不断出现注入模块错误

Keep getting injection modulerr error on AngularJS

本文关键字:注入 模块 错误 AngularJS      更新时间:2023-09-26

我使用以下代码创建一个角度应用程序,使用路由和控制器。

(function() {
    angular.module('eCommerceApp', ['ngRoute'])
        .config('$routeProvider', function($routeProvider) {
            $routeProvider.
                when('/', {
                    templateUrl: 'partials/phonelist.html',
                    controller: 'mobilePhoneListController'
                }).
                when('/phone/:phoneSlug', {
                    templateUrl: 'partials/phonedetail.html',
                    controller: 'mobilePhoneDetailController'
                }).
                otherwise({
                    templateUrl: 'error/404.html',
                });
        })
        .controller('mobilePhoneListController', ['$http', function($http) {
            var thisObj = this;
            thisObj.mobilePhones = [];
            $http.get('/api/getallphones').then( function(data) {
                thisObj.mobilePhones = data;
            }, function(data) {
                thisObj.mobilePhones = data || "Request Data Fail!";
            });
        }])
        .controller('mobilePhoneDetailController', ['$http', function($http) {
            var thisObj = this;
        }])
})();

在此之前,我导入了3个脚本。角度,角度路线和我的应用

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
<script src="/angular/e-commerce-app.js"></script>

以及网站主体结构

<html lang="en" ng-app="eCommerceApp">
<!-- ... -->
<body ng-view>
</body>
</html>

也尝试使用<ng-view></ng-view>

但我总是犯这个错误。

错误:$injector:modulerr模块错误未能实例化模块eCommerceApp,原因是:错误:[ng:areq]http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=不是%20。。。错误(本机)在http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:6:416…

在您的网站主结构(index.html)中,使用ng-app="eCommerceApp"作为'body'标记中的属性。例如:

<body ng-app="eCommerceApp">

问题是我没有正确地包含.config文件。我使用实现了它

.config('$routeProvider', function($routeProvider) { ... });

而应该是

.config(['$routeProvider', function($routeProvider) { ... }]);

(注意[]