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'});
}]).
Forse la creazione di un altro modulo solo per la configurazione condivisa, e rendendo l'altra due sottomoduli a seconda di quello? – elias
@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'? –
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