javascript - AngularJS 服务依赖注入(inject)错误

标签 javascript angularjs

<!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 未定义 Uncaught Error :[$injector:modulerr] http://errors.angularjs.org/1.5.0/ $注入(inject)器/模块? 我将所有 Controller 都连接到模块上。我还将 myService 文件添加到了 index.html 下面是我在 Controller 中的代码:

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) {
    }]);

关于javascript - AngularJS 服务依赖注入(inject)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48756122/

相关文章:

javascript - jquery 单击事件在单击之前触发

angularjs - d3 驱动的指令转换在 ng-repeat 中不起作用

javascript - AngularJS 应用程序中的代码重复

javascript - AngularJS:在 ng-model 上搜索多个值

javascript - 将javascript中的多个选择框动态添加到DOM

javascript - 填充 Google 自定义搜索引擎

javascript - Twitter 事件触发但没有实际事件

javascript - AngularJS : ng-repeat over custom directive with isolate scope

javascript - jQuery.get() 的奇怪问题

javascript - 如何以 Angular react 形式执行最小和最大验证?