1

无论如何都可以使用字符串变量调用角度服务

$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

4 回答 4

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);
     });
}
于 2013-08-16T12:31:29.157 回答
6

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

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

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

于 2013-08-16T12:56:21.757 回答
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 }
   });
})
于 2013-08-16T12:34:08.227 回答
0

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

var yourService = eval('YourServiceName');
于 2016-08-12T17:06:04.127 回答