2013-06-20 3 views
10

Disons que j'ai un module stuff que je veux injecter dans myApp config:partage entre les modules avec AngularJS?

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

    }]); 

Il y a deux sous-modules:

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

Voici la première:

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

Et la la seconde est identique pour la facilité de l'exemple:

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

Ce que vous remarquerez est que vous pouvez accéder à tous les deux en tant que fournisseurs de configurer les options:

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

Si nous étions dans un contrôleur, vous risquez d'écraser par la portée comme:

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

Mais que se passe-t-il s'ils partagent toujours la même propriété? Comment partager quelque chose entre les fournisseurs?

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

Puis-je injecter $stuff et config une propriété partagée pour les $thing1 et $thing2? Comment puis-je accéder à $thing1 et $thing2 en tant qu'extension d'un seul module? Injecter un module dans les deux qui partagent ces propriétés.

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

créer peut-être un autre module juste pour la configuration partagée, et de faire l'autre deux sous-modules en fonction de cela? – elias

+0

@elias Mais si ce sous-module ne fait rien d'autre que d'inclure la configuration, il semble plutôt sale? Et comment ferais-je quelque chose comme '$ stuff. $ Thing1'? –

+0

Je ne suis pas très familier avec la façon dont les modules sont censés fonctionner dans AngularJS, mais la façon dont je pensais que le sous-module config serait injecté à la fois dans le contrôleur et dans $ thing1 et $ thing2. Dans le contrôleur, vous feriez '$ stuff. $ Config.options ({apiKey: '23j4las'})' et vous utiliseriez '$ stuff.thing1.thing1()' et '$ stuff.thing2.thing2() 'normalement. Cela a-t-il du sens? – elias

Répondre

4

Utilisez la classe fournisseur pour remplacer les propriétés ou les instancier de toute portée:

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 sauce secrète: $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); 
      } 
     }; 
    }]; 
}); 
Questions connexes