1

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 }
   });
})
3
  • Your conditional is performing an assignment as opposed to a comparison (if(type=$scope.serviceList[index]) vs. if(type == $scope.serviceList[index])). Is that intentional? Commented Aug 16, 2013 at 11:45
  • Perhaps a better question is why are you trying to do this? How are you attempting to use getDetails()? Commented Aug 16, 2013 at 11:52
  • my type data is coming from backend its not fixed. if i will get deferment service list based on the condition i have to call the service Commented Aug 16, 2013 at 12:11

4 Answers 4

7

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);
     });
}
Sign up to request clarification or add additional context in comments.

Comments

6

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 :)

Comments

0
$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 }
   });
})

Comments

0

You can use javascript eval function, like this:

var yourService = eval('YourServiceName');

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.