我开始使用Angular并遇到问题。我不确定我是否理解模块依赖性是如何工作的。
创建模块时,传入一组必需的模块(包含服务),对吧?当您在该模块中实际使用控制器时,您将其传递给您在所需模块中定义的服务功能。
这是我在申请中遇到的错误:
Failed to instantiate module MyApp due to:
Error: [$injector:modulerr]
这是我的HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>MyApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/foundation.min.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body data-ng-app="MyApp">
<div data-ng-controller="MyAppCtrl">
<div class="three columns">
<h1 id="logo">LOGO</h1>
</div>
<div class="nine columns">
<div id="settings-nav" class="row">
<select class="three columns">
<option value="settings">My Settings</option>
<option value="users">Add/Edit Users</option>
<option value="account">Account Settings</option>
</select>
</div>
<div class="row">
<nav id="main-nav">
<ul class="six columns">
<li><a id="machines-link" class="button" href="/machines">Machines</a></li>
<li><a id="customers-link" class="button" href="/customers">Customers</a></li>
<li><a id="inspections-link" class="button" href="/inspections">Inspections</a></li>
</ul>
<div class="three columns">
<form id="search">
<input type="text" placeholder="search" />
</form>
</div>
</nav>
</div>
<div data-ng-view id="template-wrapper"></div>
</div>
</div>
<!-- required libraries. -->
<script src="js/vendor/angular.min.js"></script>
<script src="js/vendor/angular-route.min.js"></script>
<script src="js/vendor/angular-animate.min.js"></script>
<script src="js/vendor/angular-resource.min.js"></script>
<script src="js/vendor/underscore.min.js"></script>
<!-- services for each controller. each are under their own module. these talk to the rest server. -->
<script src="js/services/customer.js"></script>
<script src="js/services/inspection.js"></script>
<script src="js/services/machine.js"></script>
<!-- sets up the main MyApp module and dependencies. -->
<script src="js/setup.js"></script>
<!-- controllers for each view. all are under the MyApp module. these are loaded automatically with angular's routing. -->
<script src="js/controllers/customers-list.js"></script>
<script src="js/controllers/inspections-list.js"></script>
<script src="js/controllers/machines-list.js"></script>
<script src="js/controllers/customer.js"></script>
<script src="js/controllers/inspection.js"></script>
<script src="js/controllers/machine.js"></script>
<!-- routing config and main modustri controller. -->
<script src="js/routing.js"></script>
<script src="js/controllers/MyApp.js"></script>
</body>
</html>
我定义了这样的服务模块:
var MachineServices = angular.module('MachineServices', ['http']);
MachineServices.factory('Rest', function($http){
return{
//get machine from server
//save machine to server
//delete machine from server
//get machine list from server
}
});
在setup.js中,加载了带有服务的模块后,我创建了我的主模块:
var MyApp = angular.module('MyApp', ['ngRoute', 'ngAnimate', 'CustomerServices', 'InspectionServices', 'MachineServices']);
创建这些模块及其包含服务的方式有问题。当我将它们作为依赖项删除时,我不会再出错。
我在这里缺少什么?
提前致谢。
答案 0 :(得分:8)
我认为您的问题可能在您的模块定义中:
var MachineServices = angular.module('MachineServices', ['http']);
您无需依赖http
。 $http
服务是Angular核心的一部分,因此它不是您需要导入的模块。这样做会给你一个错误。
我还应该互换地修改您对“服务”和“模块”的使用。模块是工厂,服务,指令,控制器,过滤器等的集合,可以注入其他模块以供使用。 “服务”在Angular中具有特定含义,并且比“模块”更精细。