繁体   English   中英

AngularJS服务依赖项注入错误

[英]AngularJS service dependency injection error

 <!DOCTYPE html> <html ng-app="myApp"> <head></head> <head> </head> <!-- ControllerAs syntax --> <!-- Main controller with serveral data used on diferent view --> <body ng-controller="MainCtrl as main" class="{{$state.current.data.specialClass}}" landing-scrollspy id="page-top"> <!-- Main view --> <div ui-view></div> <!-- jQuery and Bootstrap --> <script src="js/jquery/jquery-3.1.1.min.js"></script> <script src="js/plugins/jquery-ui/jquery-ui.min.js"></script> <script src="js/bootstrap/bootstrap.min.js"></script> <!-- Anglar App Script --> <script src="js/app.js"></script> <script src="js/config.js"></script> <script src="js/controllers.js"></script> <script src="js/services/myService.js"></script> </body> </html> 

我有一个angularJS应用,在添加服务之前运行良好。 我收到以下错误。 myService.js:3未捕获的参考错误:未定义myApp未捕获的错误:[$ injector:modulerr] http://errors.angularjs.org/1.5.0/ $ injector / modulerr? 我将所有控制器都绑定到该模块。 我还已将myService文件添加到index.html,以下是我在控制器中拥有的代码:

function MainCtrl($scope, $rootScope, $http, $state, myService){
};

angular
    .module('myApp')
    .controller('MainCtrl', MainCtrl)
    .controller('FinalViewCtrl', FinalViewCtrl);

 This is what I have in myService.js 'use strict'; myApp.factory('myService', ['$http', function($http) { }]); 

让我知道我是否想念什么?

在服务代码上方添加myapp变量启动。

var myapp = angular.module('myApp');
   myApp.factory('myService', ['$http', function($http) {
    }]);

如果没有html,将很难分辨出来,但是您是否以正确的顺序添加了JavaScript文件? 您必须确保在所有控制器或服务之前加载了带有angular.module('myApp')文件。 在我的应用程序之前添加服务时,我遇到了同样的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM