Search code examples
angularjselectronangular-moduleangular-factory

angular, how to combine modules


I have a page html1.html with the module mod1

[html1.html]
<html lang="en" ng-app="mod1">
...
<script src="scripts/services/Data.js"></script>

And Data.js is a factory for mod1

[Data.js]
angular.module('mod1').factory('Data',[function(){
...
}]);

Then I have the same with a second page html2.html with the module mod2, and there I also want to use the same factory than before

[html2.html]
<html lang="en" ng-app="mod2">
...
<script src="scripts/services/Data.js"></script>
<script src="scripts/services/Train.js"></script>

Train.js is a factory for mod2, and then to combine mod1 and mod2 I do the following

[Train.js]
var mod2 = angular.module('mod2',[angular.module('mod1')]);
mod2.factory('Train',[function(){
...
}]);

But angular doesn't find the module mod1. What am I missing?

*Both html1.html and html2.html are renderers from the same Electron main application


Solution

  • Module dependencies are specified by name, so in Train.js

    angular.module('mod2', ['mod1'])
    

    You just need to include the file that defines mod1, ie the file that has

    angular.module('mod1', ['maybe', 'some', 'deps', 'here'])
    

    You include that file in html2.html as well as any other files with providers (like Data.js) and everything in that module will be available.

    Presumably you already defined the module mod1 in Data.js or another file. Defining it again as you were in Train.js overrides the registered name with a new, empty one.