Imagine we have two distinct AngularJS controllers housed in separate files that are both referenced in an HTML document. Here's how it looks:
//controller1.js
"use strict";
var someApp = angular.module('MYAPP');
//var someApp = angular.module('MYAPP',['ngCookies']); <-- Does not work
someApp.controller('Controller1', function($scope) {
$scope.CookieFunction = function(){
//foo
};
});
//controller2.js
"use strict";
var someApp = angular.module('MYAPP',['ngCookies','ui.bootstrap']);
someApp.controller('Controller2', function($scope,$cookies) {
$scope.SomeOtherfunction = function(){
//foo
};
});
//HTML file
<script src="controller1.js"></script>
<script src="controller2.js"></script>
Within controller1, there is a need to execute Cookie operations which necessitates the inclusion of ngCookies. However, if this inclusion occurs inside controller1.js, it results in controller2 becoming undefined. The preference lies in adding modules precisely where they are required and not elsewhere. How can this be accomplished without impacting subsequent controllers?
EDIT: Upon moving the inclusion of ngCookies from controller2 to controller1, the following error is encountered (Argument 'controller2' is not a function, got undefined)