vishnu
vishnu

Reputation: 4599

calling function inside angular factory

I have a factory, how do i call 'getAccountDetails' service inside getAccountInformation() function.

I tried in the following way, but not working.

AccountService.getAccountDetails.getAccountDetailsService

factory

tellerApp.factory('AccountService',['$resource', function ($resource) {
    return{
        getAccountDetails: $resource(XXX, {}, {
            getAccountDetailsService: {}
        }),
        getAccountInformation: function($scope, number, transaction, index){
            AccountService.getAccountDetails.getAccountDetailsService({number : number})
           .$promise.then(function(response){});       
    }
}]);

Upvotes: 0

Views: 402

Answers (2)

Tiesselune
Tiesselune

Reputation: 1711

I suggest you define your code dependencies out of the returned provider:

tellerApp.factory('AccountService',['$resource', function ($resource) {

    var getAccountDetails = $resource(XXX, {}, {getAccountDetailsService: {}});

    return {
        getAccountDetails : getAccountDetails,
        getAccountInformation: function($scope, number, transaction, index){
            getAccountDetails.getAccountDetailsService({number : number}).$promise.then(function(response){
                 ...
            })
        }      
    };
}]);

Or, inside an object, you can also use this to refer to the current object, instead of using AccountService.getAccountDetails, you should use this.getAccountDetails.

tellerApp.factory('AccountService',['$resource', function ($resource) {

    return {
        getAccountDetails : $resource(XXX, {}, {getAccountDetailsService: {}});,
        getAccountInformation: function($scope, number, transaction, index){
            this.getAccountDetails.getAccountDetailsService({number : number}).$promise.then(function(response){
                 ...
            })
        }      
    };
}]);

Plus, be careful, because your naming convention is confusing (getAccountDetails is not a function, since you don't call it with (), but it is named "get", getAccountServices is first defined as an object, but the same name is used later for a funtion...), especially if you want an accurate answer ;)

Upvotes: 1

Kiachma
Kiachma

Reputation: 11

This should work, havent tested it though.

tellerApp.factory('AccountService',['$resource', function ($resource) {
    var AccountService = {
        getAccountDetails: $resource(XXX, {}, {
            getAccountDetailsService: {}
        }),
        getAccountInformation: function($scope, number, transaction, index){
            AccountService.getAccountDetails.getAccountDetailsService({number : number}).$promise.then(function(response){
            }
        };       

    }
    return AccountService
}]);

Upvotes: 0

Related Questions