2013-06-20 110 views
10

比方說,我有一個stuff模塊,我希望注入myApp配置:與AngularJS模塊共享?

angular.module('myApp', ['stuff']). 
    config([function() { 

    }]); 

有兩個子模塊:

angular.module("stuff", ["stuff.thing1","stuff.thing2"]); 

這是第一個:

angular.module('stuff.thing1', []).provider("$thing1", function(){ 
    var globalOptions = {}; 
    this.options = function(value){ 
     globalOptions = value; 
    }; 
    this.$get = ['$http',function ($http) { 
     function Thing1(opts) { 
      var self = this, options = this.options = angular.extend({}, globalOptions, opts); 
     } 
     Thing1.prototype.getOptions = function(){ 
      console.log(this.options.apiKey); 
     }; 
     return { 
      thing1: function(opts){ 
       return new Thing1(opts); 
      } 
     }; 
    }]; 
}); 

而第二個是相同的,以便於舉例:

angular.module('stuff.thing2', []).provider("$thing2", function(){ 
    var globalOptions = {}; 
    this.options = function(value){ 
     globalOptions = value; 
    }; 
    this.$get = ['$http',function ($http) { 
     function Thing2(opts) { 
      var self = this, options = this.options = angular.extend({}, globalOptions, opts); 
     } 
     Thing2.prototype.getOptions = function(){ 
      console.log(this.options.apiKey); 
     }; 
     return { 
      thing2: function(opts){ 
       return new Thing2(opts); 
      } 
     }; 
    }]; 
}); 

,你會發現什麼是你可以爲供應商配置選項的訪問他們兩個:

angular.module('myApp', ['stuff']). 
    config(['$thing1Provider', '$thing2Provider', function($thing1Provider, $thing2Provider) { 
    $thing1Provider.options({apiKey:'abcdef'}); 
    $thing2Provider.options({apiKey:'abcdef'}); 
    }]); 

如果我們是在一個控制器,你可以每範圍覆蓋,如:

controller('AppController', ['$scope','$thing1', function($scope, $thing1) {  
    var thing1 = $thing1.thing1({apiKey:'3kcd894g6nslx83n11246'}); 
}]). 

但是如果他們總是分享相同的財產呢?我如何在提供者之間分享內容?

angular.module('myApp', ['stuff']).config(['$stuff' function($stuff) { 
    //No idea what I'm doing here, just trying to paint a picture. 
    $stuff.options({apiKey:'abcdef'}); 
}]); 

我可以注入$stuff和配置兩個$thing1$thing2共同財產?

如何作爲單個模塊的擴展訪問$thing1$thing2

controller('AppController', ['$scope','$stuff', function($scope, $stuff) { 
    //Again - no idea what I'm doing here, just trying to paint a picture. 

    //$thing1 would now be overwrite $stuff.options config above. 
    var thing1 = $stuff.$thing1.thing1({apiKey:'lkjn1324123l4kjn1dddd'}); 

    //No need to overwrite $stuff.options, will use whatever was configured above. 
    var thing2 = $stuff.$thing2.thing2(); 

    //Could I even change the default again for both if I wanted too? 
    $stuff.options({apiKey:'uih2iu582b3idt31d2'}); 
}]). 
+0

也許創造另一個模塊只是爲了共享配置,並使得其他兩個子模塊取決於它? – elias

+0

@elias但是,如果該子模塊除了包含配置之外不做任何事情,它看起來有點髒麼?我該怎麼做'$ stuff。$ thing1'? –

+0

我對AngularJS中的模塊應該如何工作並不是很熟悉,但是我認爲配置子模塊的方式將被注入到控制器和$ thing1和$ thing2中。在控制器中,你可以執行$ stuff。$ config.options({apiKey:'23j4las'})'然後使用'$ stuff.thing1.thing1()'和'$ stuff.thing2.thing2() 「通常。那有意義嗎? – elias

回答

4

將模塊注入到兩個共享這些屬性的模塊中。

使用提供類覆蓋的屬性或任何範圍實例他們:

angular.module("stuff.things", []).provider("$things", function(){ 
    var globalOptions = {}; 
    this.options = function(value){ 
     globalOptions = value; 
    }; 
    this.$get = [, function() { 
     function Things(opts) { 
      var self = this, options = this.options = angular.extend({}, globalOptions, opts); 
     } 
     Things.prototype.returnOptions = function(){ 
      return this.options; 
     }; 
     return { 
      things: function(opts){ 
       return new Things(opts); 
      } 
     }; 
    }]; 
}); 

的祕密武器:$things.things().returnOptions()

angular.module('stuff.thing1', ['stuff.things']).provider("$thing1", function(){ 
    var globalOptions = {}; 
    this.options = function(value){ 
     globalOptions = value; 
    }; 

    this.$get = ['$things', function ($things) { 
     function Thing1(opts) { 
      var self = this, options = this.options = angular.extend({}, $things.things().returnOptions(), globalOptions, opts); 
     ... 
     } 
     return { 
      thing1: function(opts){ 
       return new Thing1(opts); 
      } 
     }; 
    }]; 
});