如何使用字符串变量调用“Angular JS”服务方法

时间:2013-08-16 11:42:40

标签: angularjs angularjs-service

无论如何都使用字符串变量

来调用角度服务
$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 }
   });
})

4 个答案:

答案 0 :(得分:7)

我认为你可能会使事情变得复杂,让事情变得简单。

创建一个detailsService,其中包含您需要的所有方法。

.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
                }
            })
        });

在您的控制器中,您可以通过key

访问该方法
$scope.getDetails=function(type,index){

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

答案 1 :(得分:6)

直接回答您的问题,即如何通过字符串获取angularJS服务。

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

但是,不要像@Mark在他的回答中所说的那样复杂化:)

答案 2 :(得分: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 }
   });
})

答案 3 :(得分:0)

您可以使用javascript eval function,如下所示:

var yourService = eval('YourServiceName');