Angular Zen
}
});
angular.module('engines', [])
.factory('dieselEngine', function () {
return {
type: 'diesel'
};
});
Here the car service is defined in the app module. The app module declares a
dependency on the engines module, where the dieselEngine service is defined.
Not surprisingly a car can be injected with an instance of an engine.
Perhaps more surprisingly, services defined on sibling modules are also visible to
each other. We could move a car service into a separate module, and then change
module dependencies, so that an application depends on both the engines and
cars modules as follows:
angular.module('app', ['engines', 'cars'])
angular.module('cars', [])
.factory('car', function ($log, dieselEngine) {
return {
start: function() {
$log.info('Starting ' + dieselEngine.type);
}
};
});
angular.module('engines', [])
.factory('dieselEngine', function () {
return {
type: 'diesel'
};
});
In the preceding case an engine can still be injected into a car without any problem.
A service defined in one of the application's modules is visible to all the
other modules. In other words, hierarchy of modules doesn't influence
services' visibility to other modules. When AngularJS bootstraps an
application, it combines all the services defined across all the modules
into one application, that is, global namespace.