Uncaught Error: [$injector:nomod] while Angularjs separating code into Controller and Service files

Uncaught Error: [$injector:nomod] while Angularjs separating code into Controller and Service files

我正在尝试将代码拆分为控制器和服务文件,如下所示。

Module.js

    /// <reference path="../angular.js" />
    /// <reference path="../angular-resource.js" />
    var IM_Mod_app;
    (function () {
  //   IM_Mod_app = angular.module('IM_ng_app', []);
    IM_Mod_app = angular.module('IM_ng_app', ['IM_Mod_app.controllers']);
})();

Controller.js

    /// <reference path="../angular.js" />
    /// <reference path="../angular-resource.js" />
    /// <reference path="module.js" />
    /// <reference path="service.js" />

   // angular.module('IM_Mod_app.controllers', []);
    angular.module('IM_Mod_app.controllers').controller('IM_Ctrl', ['$scope', '$http', 'MaintenanceService', function ($scope, $http, MaintenanceService) {

        IM_Ctrl.$inject = ['$scope', '$http']; var PlantId = "DFC";

        loadRecords();

            function loadRecords() {

                var promiseGet = MaintenanceService.GetAllFlavors();

                promiseGet.then(function successCallback(response) {
                    alert(response.data);
                    $scope.flavours = response.data;
                }, function errorCallback(response) {      
                });
            }

            function onFlavorChange() {
                var promiseonchange = MaintenanceService.onchangeflr();

                promiseonchange.then(function successCallback(response) {
                   // alert(response.data);
                    $scope.items = response.data;
                }, function errorCallback(response) {  });
            }
        }]);

Service.js:

/// <reference path="../angular.js" />
/// <reference path="../angular-resource.js" />
/// <reference path="module.js" />
/// <reference path="controller.js" />

IM_Mod_app.service("MaintenanceService", function ($http) {

    this.GetAllFlavors = function () {
        //alert("service");
        return $http({
            method: 'GET',
            url: 'http://localhost:55762/api/ItemMaintenance/GetAllFlavors',
            params: { Plant_Id: "DFC" }
        })
    }

    this.onchangeflr = function () {
        $http({
            method: 'GET',
            url: 'http://localhost:55762/api/ItemMaintenance/GetAllFilteredItems',
            params: { Plant_Id: PlantId, Flavor_Id: flv, Variant_Id: '', Brand_Id: '' }
        })
    }
});

<body ng-app="IM_ng_app">
    <table ng-controller="IM_Ctrl">
        <tr>
            <td>
                <select ng-model="flv" ng-options="flv.FLAVOR_ID for flv in flavours" ng-change="onFlavorChange(flv.FLAVOR_ID)">
                    <option value="">Select Flavor</option>
                </select>
</td>
</tr>
</table>
</body>

Service.js:6 Uncaught ReferenceError: IM_Mod_app is not defined at Service.js:6

收到错误消息为:

"angular.js:138 Uncaught Error: [$injector:nomod] Module 'IM_Mod_app.controllers' is not available! You either misspelled the module name or forgot to load it."

我认为您缺少一些注射剂。

在应用程序模块。

IM_Mod_app = angular.module('IM_ng_app', ['IM_Mod_app.controllers']); }

在控制器(在 $http 之后)

angular.module('IM_Mod_app.controllers', []).controller('IM_Ctrl', ['$scope', '$http', 'MaintenanceService', function ($scope, $http, MaintenanceService) {

这样试试。

Module.js

/// <reference path="../angular.js" />
/// <reference path="../angular-resource.js" />//
var IM_Mod_App = angular.module("IM_ng_App", []);

Controller.js

/// <reference path="../angular.js" />
/// <reference path="../angular-resource.js" />
/// <reference path="module.js" />
/// <reference path="service.js" />
IM_Mod_App.controller("IM_ng_Ctrl", function ($scope, $http, IM_Service) { alert("Hi controller");... });

Service.js

/// <reference path="../angular.js" />
/// <reference path="../angular-resource.js" />
/// <reference path="module.js" />
/// <reference path="controller.js" />
var IM_Mod_App = angular.module("IM_ng_App");
IM_Mod_App.factory('IM_Service', function ($http) {
        alert("Hi service");
});

尝试交叉验证控制器和模块的名称。