Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a base.js
and a services.js in My angular App
but when I move the code to Services.js. The Application Breaks

App.js - This Works
JavaScript
var App = angular.module("App", ["ui.router", "ui.bootstrap", "ngAnimate","toastr"])

.service("utilServices", function ($rootScope, toastr) {
var mem = {};
return {
    storeVal: function (key, value) {
        mem[key] = value;
    },
    fetchVal: function (key) {
        return mem[key];
    },
    showAlert: function (msg, msgType, appVal) {
        if (msgType === "success") {
            return toastr.success(msg, appVal);
       }
        return false;
    }
};
});
App.service("alertService", function (utilServices) {
var appVal = utilServices.fetchVal("vTitle") + " says";
this.Display = function (msg,msgtype) { return utilServices.showAlert(msg,msgtype,appVal); };

return false;
});
App.controller("baseController", window.baseController);

But the same when I move the services to a different file "Services.js"
..........App.js -
JavaScript
var App = angular.module("App", ["ui.router", "ui.bootstrap", "ngAnimate", "toastr"]);

App.service("utilServices", window.commonServices);

App.controller("baseController", window.baseController);

.........Services.js
JavaScript
 var mem = {};
 window.commonServices = function($rootScope, toastr) {
   return {
    storeVal: function(key, value) {
        mem[key] = value;

    },
    fetchVal: function(key) {

        return mem[key];
    },
    showAlert: function(msg, msgType, appVal) {
        if (msgType === "success") {
            return toastr.success(msg, appVal);
        }
        return false;
    }
};


};
window.commonServices.$inject = ["$rootScope", "toastr"];

I get this error in the developer console of My Browser (Chrome) -
TypeError: Cannot read property 'prototype' of undefined
at Object.instantiate (angular.js:4485)
at Object.<anonymous> (angular.js:4346)
at Object.invoke (angular.js:4478)
at Object.enforcedReturnValue [as $get] (angular.js:4330)
at Object.invoke (angular.js:4478)
at angular.js:4295
at getService (angular.js:4437)
at Object.invoke (angular.js:4469)
at extend.instance (angular.js:9136)
at nodeLinkFn (angular.js:8248)

This only occurs when I move the code of service to a separate a file and not when it is declared within the app.js file. I think it has something to do with the $.inject. I'm still a newbie learning the ropes. Any Help would be greatly appreciated :)
Posted
Updated 14-Nov-15 8:41am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900