1

I'm trying to split the code into Controller and Service files as below.

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

Getting error message as:

"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."

2 Answers 2

0

Try like this.

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");
});

Try to cross verify the names of the controller and module.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are missing some injections.

At the app module.

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

At the controller (after $http)

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

6 Comments

Thxs looking into it.. still having issue with error msg: Uncaught Error: [$injector:nomod] Module 'IM_Mod_app.controllers' is not available!.
Can you please add the code that defines IM_Mod_app module?
'IM_ng_app' should inject 'IM_Mod_app.controllers'. I updated the answer.
Thxs. Still same issue exits.
mmm... sorry to hear that. It's most likely an issue at this area. It may be the order in which you define the modules, a missing injection, or some error within a module.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.