2

My code is like this

Services.JS
    angular.module('RateRequestApp.services', []).factory('rateRequestService', ['$http', rateRequestService]);

function rateRequestService($http) {
    var service = { getData: getData};
    return service;
    function getData() {
        return $http({
            method: 'Get',
            url: '../services/asas.asmx/GetReadOnlyData?shipmentID=' + getQuery('shipmentId')
        });
    }
    function getQuery(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }
}

angular.module('RateRequestApp.services', []).factory('rePrintService', ['$http', rePrintService]);
function rePrintService($http) {
    var service = { getReprintData: getReprintData };
    return service;
    function getReprintData() {
        return $http({
            method: 'Get',
            url: '../services/asas.asmx/RePrint?shipmentID=' + getQuery('shipmentId')
        });
    }
    function getQuery(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }

}


Controller.JS


    angular.module('RateRequestApp.controllers', []).controller
('ReadOnlyController', [
    '$scope', 'rateRequestService', 'rePrintService', '$controller',
    function($scope, rateRequestService, rePrintService, $controller) {
        $scope.rateData = [];
        rateRequestService.getData().success(function(response) {
                $scope.rateData = response;
                console.log(JSON.stringify(response));
            }).
            error(function(data, status, headers, config) {});
        rePrintService.getReprintData().success(function(response) {
            }).
            error(function(data, status, headers, config) {});

    }
]);

App.js
angular.module('RateRequestApp', [
   'RateRequestApp.services',
   'RateRequestApp.controllers',
   'ui.bootstrap'
]);

Everything looks okay to me, But this throws an error

Error: [$injector:unpr] Unknown provider: rateRequestServiceProvider <- rateRequestService

When I Inject only my first service, It works correct. but when second comes it breaks. Can any one point out what I am doing wrong?

2 Answers 2

4

The problem is that you have two modules

angular.module('RateRequestApp.services', [])

definitions, so the second one overwrites the first one which holds rateRequestService service.

To properly retrieve existent module you need to use getter syntax:

angular.module('RateRequestApp.services').factory('rePrintService', ['$http', rePrintService]);

Note that there is no dependency array [] after module name.

Or you can use chain notation like this:

angular.module('RateRequestApp.services', [])
.factory('rateRequestService', ['$http', rateRequestService])
.factory('rePrintService', ['$http', rePrintService]);
Sign up to request clarification or add additional context in comments.

2 Comments

Can you add a solution here, where I can use both my services?
@Athul in your question above you just defined your two services and never used it, maybe you have to provide further information about your usage, i.e. your controller where you use your services.
-2

First thing that I am noticing is that the code itself needs to be better organized to be honest, but anyways. Second thing is that you need to add the module that you created into your "RateRequestApp.controllers" module. So that the code entirely would be something like:

angular.module('RateRequestApp.controllers', ['RateRequestApp.services']).controller

This + the previous answer should do the trick. good luck

Comments

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.