2015-06-12 136 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