簡體   English   中英

使用AngularJS在模塊之間共享?

[英]sharing between modules with AngularJS?

假設我有一個我要注入myApp配置的stuff模塊:

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:'01234569abcdef'});
    $thing2Provider.options({apiKey:'01234569abcdef'});
  }]);

如果我們在控制器中,您可以覆蓋每個范圍,如:

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

我可以為$thing1$thing2注入$stuff並配置共享屬性嗎?

如何訪問$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'});
}]).

將模塊注入共享這些屬性的模塊。

使用provider類覆蓋屬性或從任何范圍實例化它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM