See this plunker code (Notice console.log messages) to understand what I am trying to say/ask.
I have defined 3 modules, namely, myApp, myApp.view1, myApp.view2. Only myApp module has dependency declared on the other 2.
myApp Module
angular.module('myApp', ['ngRoute','myApp.view1','myApp.view2'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.otherwise({redirectTo: '/view1'});
}])
.value('author', 'Suman Barick')
myApp.view1 Module
angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {
template: 'Welcome to view ONE',
controller: 'View1Ctrl'
});
}])
.controller('View1Ctrl', ['author', function(author) {
console.log('*******************************************');
console.log('I am on view1 module');
console.log('Printing author value from myApp module ' + author);
console.log('*******************************************');
}])
.value('view1_var', 'Hi, I am defined as value in myApp.view1 module')
.service('serviceV1', function(){
this.getData = function(){return 'abcdef';}
console.log('This is a service from myApp.view1 module');
})
myApp.view2 Module
angular.module('myApp.view2', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view2', {
template: 'Welcome to view TWO',
controller: 'View2Ctrl'
});
}])
.controller('View2Ctrl', ['view1_var','serviceV1', function(view1_var, serviceV1) {
console.log('*******************************************');
console.log('Look I am accessing view1_var and serviceV1 of view1 module... from view2 module');
console.log(view1_var);
console.log(serviceV1.getData());
console.log('*******************************************');
}]);
My Doubts / Questions:
Why can "myApp.view1" module access the "author" value defined on "myApp" module. "myApp" has a dependency on "myApp.view1", but not vice versa.
More interestingly, "myApp.view1" and "myApp.view2" are 2 entirely separate module. Then how come "myApp.view2" is accessing the
view1_varandserviceV1from "myApp.view1" without even declaring any dependency on it?Is this the intended design / behavior ? Then what are other things that I can define in one module but use it in any other module irrespective of their dependency among themselves?
Can someone please explain?
