Upon diving into the AngularJS documentation and development guide on dependency injection, I was greeted with a syntax that seemed unfamiliar compared to what I had previously learned.
The concepts of factory methods and module methods introduced at the beginning of the dependency injection guide left me puzzled.
An excerpt from the documentation provided an example:
angular.module('myModule', [])
.config(['depProvider', function(depProvider) {
// ...
}])
.run(['depService', function(depService) {
// ...
}])
With no detailed implementation offered, grasping the syntax proved to be challenging, especially since the example shown for .config
did not include square brackets during declaration as I had seen before.
I sought understanding regarding the significance of square brackets in .factory
, .directive
, and .config
, along with comprehending the syntax as a whole. It appeared markedly different from a previous example which focused on configuring services using providers.
//define a module
var mainApp = angular.module("mainApp", []);
...
//create a service using provider
//which defines a method square to return square of a number.
mainApp.config(function($provide) {
$provide.provider('MathService', function() {
this.$get = function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
};
});
});
Could the disparity between the two examples stem from them belonging to distinct versions of AngularJS?