Can I have two angular.module calls to the same ng-app in two different files?
-
20-12-2019 - |
Question
I have an angular.js app that is declared with ng-app="audioApp"
in the <body>
tag of the html file.
If I have an angular.module
call in one javascript file:
var app = angular.module('audioApp', []);
Can I make another identical angular.module
call in a different javascript file? My application is mysteriously breaking with this second angular.module
call, so it seems I can not have this call twice.
Any suggestions to a solution would be appreciated.
Solution
Yes. You need to omit the , []
part.
var app = angular.module('audioApp');
That'll grab a reference to the module, rather than redefine it.
What I like to do is keep a module.js
file, which comes before any files which depend on the module or extend it.
audioApp.js
var app = angular.module('audioApp', ['some', 'deps']);
someDirective.js
var app = angular.module('audioApp');
app.directive(...);
someController.js
var app = angular.module('audioApp');
app.controller();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow