gpt4 book ai didi

javascript - 使用 AngularJS 在模块之间共享?

转载 作者:可可西里 更新时间:2023-11-01 02:45:49 25 4
gpt4 key购买 nike

假设我有一个 stuff 模块,我想将其注入(inject)到 myApp 配置中:

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 中,您可以像这样覆盖每个范围:

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

但是如果他们总是共享相同的属性(property)呢?我如何在提供商之间共享某些内容?

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

我可以注入(inject) $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'});
}]).

最佳答案

将一个模块注入(inject)到共享这些属性的两者中。

使用提供者类覆盖属性或从任何范围实例化它们:

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

关于javascript - 使用 AngularJS 在模块之间共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com