2015-06-12 70 views
0

我想将所有常量存储在一个地方,并在需要的地方注入此模块。使用当前模块服务中其他模块的常量

constant.js

(function(){ 

     var constantModule = angular.module('Constants',[]); 
     constantModule.constant('AppDefaults',{ 
      DEFAULT_PAGE_SIZE : 100 
     }); 

    }()); 

,并使用这个常量在这里另一个模块:

 var app = angular.module('app',['Constants']); 
    app.controller('AppController',function($scope,AppService){ 

    $scope.someMethod = function() { 
     var response = AppService.doSomething(); 
    } 

    }); 
    app.service('AppService',['$http', '$q','Constants',function($http,$q,Constants){ 

    return({ 
    doSomething:doSomething 
    }); 

function doSomething() { 
    method: "post", 
     url: "/foo/bar", 
     data: { 
     pageSize: Constants.AppDefaults.DEFAULT_PAGE_SIZE 
     } 
} 
}]); 

但我的角度不能注入常量到服务。有没有解决的办法 ?

回答

1

模块无法注入。然而,一个常数可以作为一项服务:

app.service('AppService',['$http', '$q', 'AppDefaults', function($http, $q, AppDefaults) { 

    return({ 
     doSomething:doSomething 
    }); 

    function doSomething() { 
     return $http({ 
      method: "post", 
      url: "/foo/bar", 
      data: { 
       pageSize: AppDefaults.DEFAULT_PAGE_SIZE 
      } 
     }); 
    } 
}]); 
+0

那么这是令人尴尬的,但谢谢。 – Hav3n