Francis Stalin
Francis Stalin

Reputation: 449

How to call "Angular JS" service method using string variable

is there anyway to call angular service using string variable

$scope.serviceList=["yearDetails","monthDetails","dayDetails"];

//controller
$scope.getDetails=function(type,index){

    if(type==$scope.serviceList[index]){

    // if i will call like this yearDetails.query(function(data){}); it is working
    //here i am getting "yearDetails"
     $scope.serviceList[index].query(function(data){
            console.log(data);
     });
    }
}

//service
.factory('yearDetails', function($resource){
           return $resource('/getyearDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('monthDetails', function($resource){
           return $resource('/getmonthDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('dayDetails', function($resource){
           return $resource('/getdayDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})

Upvotes: 1

Views: 6172

Answers (4)

ggomesfe
ggomesfe

Reputation: 343

You can use javascript eval function, like this:

var yourService = eval('YourServiceName');

Upvotes: 0

Tzu ng
Tzu ng

Reputation: 9244

To answer your question directly that how to get angularJS service by its string.

// inject $injector to your controller
var yourService = $injector.get('YourServiceName');

But still, don't complicate things like @Mark has said in his answer :)

Upvotes: 6

Francis Stalin
Francis Stalin

Reputation: 449

$scope.serviceList=["yearDetails","monthDetails","dayDetails"];

//controller
$scope.getDetails=function(type,index){

    if(type==$scope.serviceList[index]){

     //now i can able to call my service through injector in angular
     var $inj = angular.element('html').injector();
     var serv = $inj.get($scope.serviceList[index]);
     serv.query(function(data){
            console.log(data);
     });
    }
}

//service
.factory('yearDetails', function($resource){
           return $resource('/getyearDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('monthDetails', function($resource){
           return $resource('/getmonthDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})
.factory('dayDetails', function($resource){
           return $resource('/getdayDetails', {}, {
               query: { method:'POST', params:{}, isArray:false }
   });
})

Upvotes: 0

Mark Coleman
Mark Coleman

Reputation: 40863

I think you might be over complicating things, keep things simple.

Create a detailsService that contains all methods you require.

.factory('detailsService', function ($resource) {
        return {
            yearDetails: $resource('/getyearDetails', {}, {
                query: {
                    method: 'POST',
                    params: {},
                    isArray: false
                }
            }),
            monthDetails: $resource('/getmonthDetails', {}, {
                query: {
                    method: 'POST',
                    params: {},
                    isArray: false
                }
            }),
            dayDetails: $resource('/getdayDetails', {}, {
                query: {
                    method: 'POST',
                    params: {},
                    isArray: false
                }
            })
        });

And in your controller you can access the method by key

$scope.getDetails=function(type,index){

    detailsService[type].query(function(data){
            console.log(data);
     });
}

Upvotes: 7

Related Questions