Sharing Between Modules With Angularjs?
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.modul
Solution 1:
Inject 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 () {
functionThings(opts) {
var self = this, options = this.options = angular.extend({}, globalOptions, opts);
}
Things.prototype.returnOptions = function(){
returnthis.options;
};
return {
things: function(opts){
returnnewThings(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) {
functionThing1(opts) {
varself = this, options = this.options = angular.extend({}, $things.things().returnOptions(), globalOptions, opts);
...
}
return {
thing1: function(opts){
returnnew Thing1(opts);
}
};
}];
});
Post a Comment for "Sharing Between Modules With Angularjs?"