3gwebtrain
3gwebtrain

Reputation: 15303

How to `$inject` factory service in to a `service module`?

I have a factory module called server'. for some reasons i have created a generic web service in aservicemodule. but when iinjectthe server module, i am getting result asundefined`

how to solve this?

this is my factory module :

(function () {

    "use strict";

    angular
        .module("tcpApp")
        .factory("server", ['$resource', function ($resource) {

            var base = 'http://azvsptcsdev02:678/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/';

            return {

                batchUpdate : $resource( base + 'UpdateImportantbyUser')

            }

        }]);

})();

this is my web service, where the server module requires.

"use strict";
angular.module("tcpApp").service("batchService", ['server', function ( $rootScope, server ) {

    console.log( server ); // consoles as undefined

    server.batchUpdate.save({ //i need to use the server serice

            "Id" : newId,
            "Type" : newType,
            "IsImportant"  : newImportant

        })
        .$promise.then(function(response) {

            console.log(response);
            object.IsImportant = newImportant;

        });

}]);

how to solve this? what is wrong here?

Upvotes: 0

Views: 20

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

here you have missed to annotate a the $rootScope dependency,

What happen in your problem is $rootScope getting the injected service and service getting nothing.

try to console.log($rootScope); it will print your injected server.

SO the correct format would be like this,

angular.module("tcpApp").service("batchService", ['$rootScope', 'server', function ( $rootScope, server ) {

for more read this document

Upvotes: 2

Related Questions