1

我从 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']);

这些模块及其包含服务的创建方式存在问题。当我将它们作为依赖项删除时,我不再收到错误消息。

我在这里想念什么?

提前致谢。

4

1 回答 1

8

我认为您的问题可能在您的模块定义中:

var MachineServices = angular.module('MachineServices', ['http']);

你不需要依赖http. 该$http服务是 Angular 核心的一部分,因此它不是您需要导入的模块。这样做会给你一个错误。

我还应该更正您交替使用“服务”和“模块”的问题。模块是工厂、服务、指令、控制器、过滤器等的集合,可以注入到其他模块中使用。“服务”在 Angular 中具有特定的含义,并且比“模块”更细化。

于 2013-09-03T20:00:10.653 回答