2013-06-20 15 views
10

Diciamo che ho un modulo di stuff che voglio iniettare in myApp config:condivisione tra moduli con AngularJS?

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

    }]); 

ci sono due sottomoduli:

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

Ecco la prima:

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); 
      } 
     }; 
    }]; 
}); 

E il la seconda è identica per semplicità di esempio:

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); 
      } 
     }; 
    }]; 
}); 

cosa che si nota è che è possibile accedere a ciascuno di essi come fornitori per configurare le opzioni:

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

Se fossimo in un controllore, si può sovrascrivere per portata come:

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

Ma cosa succede se condividono sempre la stessa proprietà? Come faccio a condividere qualcosa tra i fornitori?

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'}); 
}]); 

Posso iniettare $stuff e config un terreno comune sia per $thing1 e $thing2?

Come accedere a entrambi gli standard $thing1 e $thing2 come un'estensione di un singolo modulo?

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

Forse la creazione di un altro modulo solo per la configurazione condivisa, e rendendo l'altra due sottomoduli a seconda di quello? – elias

+0

@elias Ma se quel sottomodulo non fa altro che includere la configurazione, sembra un po 'sporco no? E come potrei fare qualcosa come '$ stuff. $ Thing1'? –

+0

Non ho molta familiarità con il modo in cui i moduli dovrebbero funzionare in AngularJS, ma il modo in cui pensavo fosse il sottomodulo di configurazione sarebbe stato iniettato sia nel controller che in $ thing1 e $ thing2. Nel controller si farebbe '$ stuff. $ Config.options ({apiKey: '23j4las'})' e quindi si userebbe '$ stuff.thing1.thing1()' e '$ stuff.thing2.thing2() 'normalmente. Ha senso? – elias

risposta

4

Iniettare un modulo in entrambi che condivide queste proprietà.

utilizzare la classe fornitore per sovrascrivere le proprietà o un'istanza da qualsiasi campo di applicazione:

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); 
      } 
     }; 
    }]; 
}); 

La salsa segreta: $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); 
      } 
     }; 
    }]; 
}); 
Problemi correlati