admin管理员组文章数量:1302359
Let's say I have a stuff
module that I want to inject into myApp
config:
angular.module('myApp', ['stuff']).
config([function() {
}]);
There are two submodules:
angular.module("stuff", ["stuff.thing1","stuff.thing2"]);
Here's the first:
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);
}
};
}];
});
And the second is identical for ease of example:
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);
}
};
}];
});
What you will notice is that you can access both of them as providers to configure options:
angular.module('myApp', ['stuff']).
config(['$thing1Provider', '$thing2Provider', function($thing1Provider, $thing2Provider) {
$thing1Provider.options({apiKey:'01234569abcdef'});
$thing2Provider.options({apiKey:'01234569abcdef'});
}]);
If we were in a controller, you could overwrite per scope like:
controller('AppController', ['$scope','$thing1', function($scope, $thing1) {
var thing1 = $thing1.thing1({apiKey:'3kcd894g6nslx83n11246'});
}]).
But what if they are always sharing the same property? How do I share something between providers?
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'});
}]);
Can I inject $stuff
and config a shared property for both $thing1
and $thing2
?
How do I access both $thing1
and $thing2
as an extension of a single module?
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'});
}]).
Let's say I have a stuff
module that I want to inject into myApp
config:
angular.module('myApp', ['stuff']).
config([function() {
}]);
There are two submodules:
angular.module("stuff", ["stuff.thing1","stuff.thing2"]);
Here's the first:
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);
}
};
}];
});
And the second is identical for ease of example:
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);
}
};
}];
});
What you will notice is that you can access both of them as providers to configure options:
angular.module('myApp', ['stuff']).
config(['$thing1Provider', '$thing2Provider', function($thing1Provider, $thing2Provider) {
$thing1Provider.options({apiKey:'01234569abcdef'});
$thing2Provider.options({apiKey:'01234569abcdef'});
}]);
If we were in a controller, you could overwrite per scope like:
controller('AppController', ['$scope','$thing1', function($scope, $thing1) {
var thing1 = $thing1.thing1({apiKey:'3kcd894g6nslx83n11246'});
}]).
But what if they are always sharing the same property? How do I share something between providers?
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'});
}]);
Can I inject $stuff
and config a shared property for both $thing1
and $thing2
?
How do I access both $thing1
and $thing2
as an extension of a single module?
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'});
}]).
Share
edited Jun 21, 2013 at 15:43
Dan Kanze
asked Jun 20, 2013 at 1:09
Dan KanzeDan Kanze
18.6k28 gold badges84 silver badges135 bronze badges
7
- Maybe creating another module just for the shared configuration, and making the other two submodules depending on that? – Elias Dorneles Commented Jun 20, 2013 at 16:10
-
@elias But if that submodule doesn't do anything but include configuration, it seems kind of dirty no? And how would I do something like
$stuff.$thing1
? – Dan Kanze Commented Jun 20, 2013 at 16:28 -
I'm not much familiar to how modules are supposed to work in AngularJS, but the way I thought was the config submodule would be injected both in the controller and in $thing1 and $thing2. In the controller you would do
$stuff.$config.options({apiKey:'23j4las'})
and then you'd use$stuff.thing1.thing1()
and$stuff.thing2.thing2()
normally. Does that make sense? – Elias Dorneles Commented Jun 20, 2013 at 16:48 - What you need is a service. This article explains how to use services to create a shared model. Modeling Data and State in Your AngularJS Application – Brian Lewis Commented Jun 20, 2013 at 17:46
- It's a bit more involved than that mate. – Dan Kanze Commented Jun 20, 2013 at 17:57
1 Answer
Reset to default 4Inject a module into both that shares these properties.
Use the provider class to overwrite properties or instantiate them from any scope:
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);
}
};
}];
});
The secret sauce: $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);
}
};
}];
});
本文标签: javascriptsharing between modules with AngularJSStack Overflow
版权声明:本文标题:javascript - sharing between modules with AngularJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741682007a2392225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论