我正在嘗試爲不同的模塊使用相同的服務。有很多模塊,所以我試圖將它們注入到父模塊中。事情是這樣的:Angularjs:在不同模塊中使用通用服務
var app=angular.module('myapp',['module_1','module_2',....,'module_n']);
var module_1=angular.module('myapp1',[]);
var module_2=angular.module('myapp2',[]);
var module_3=angular.module('myapp3',[]);
.
.
.
var module_n=angular.module('myappN',[]);
並且是通用於所有ñ模塊的服務是這樣的:
.service('myService',function(){
...doing something here...
});
現在我無法弄清楚如何使用該服務的所有子模塊。
我應該用哪個模塊關聯這個服務?我試過app.service('myService',function(){...})
,但它沒有奏效。
我哪裏錯了?
編輯1:
而且我想與使用服務所有這些子模塊共享變量。我不知道是否,我正在通過使用服務進行共享變量,或者我應該使用供應商或工廠執行此作業。
編輯2:
我發現這些鏈接,但我無法把握答案。參考它們並請提供我的回答
How to share a variable between multiple modules in AngularJS
Passing variable between controllers which are on different modules
http://stackoverflow.com/questions/16725392/share-a-single-service-between-multiple-angular-js-apps –