Alexe Bogdan
Web Developer, JavaScript Lover, User Experience Professional, working in the web and mobile application development industry.
Inversion of control is a design paradigm with the goal of giving more control to the targeted components of your application, the ones getting the work done
Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time.
In traditional programming, the flow of the business logic is determined by objects that are statically assigned to one another.
With inversion of control, the flow depends on the object graph that is instantiated by the assembler and is made possible by object interactions being defined through abstractions. The binding process is achieved through dependency injection.
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies.
The Angular injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested.
Angular modules have the opportunity to configure themselves before the module actually bootstraps and starts to run.
Configuration Block:
Run Block:
How to create a service?
angular
.module('myApp', [])
.constant('apiUrl', 'http://localhost:8080')
.config(['apiUrl', function(apiUrl){
//apiUrl can be used here
}])
.run(['$rootScope', function($rootScope){
//apiUrl can be used here
}]);
angular
.module('myApp', [])
.value('objectValue', {
foo: 'bar'
})
.run(['$rootScope', 'objectValue',
function($rootScope, objectValue){
$rootScope.foo = objectValue.foo;
}
]);
angular
.module('myApp', [])
.factory('myFactory', function(){
var data; //private variable
return {
fetchData: function(){
//business to populate data
},
getData: function(){
return data;
}
}
})
.run(['$rootScope', 'myFactory',
function($rootScope, myFactory){
myFactory.fetchData();
$rootScope.data = myFactory.getData();
}
]);
angular
.module('myApp', [])
.service('myService', function(){
var data; //private variable
this.fetchData= function(){
//business to populate data
};
this.getData= function(){
return data;
};
});
.factory('myService', function(){
var Service = function(){
var data; //private variable
this.fetchData= function(){
//business to populate data
};
this.getData= function(){
return data;
};
};
return new Service();
});
angular
.module('myApp', [])
.provider('myFactory', function(){
var configVar = 'value';
//The factory Service - can have any dependency
this.$get = ['$http', function($http){
var data; //private variable
return{
fetchData: function(){
//business to populate data
},
getData: function(){
return data;
}
};
}];
//Config method
this.config = function(config){
configVar = config;
};
})
.config(['myFactoryProvider', function(myFactoryProvider){
myFactoryProvider.config('Overriden value');
}])
.run(['$rootScope', 'myFactory',
function($rootScope, myFactory){
myFactory.fetchData();
$rootScope.data = myFactory.getData()
}
]);
ngRoute module is a core Angular module for basic routing. The module provides the ng-view directive, in order to render the appropriate template.
Any time the route is changed the directive will update it's content:
$routeProvider
.when('path', {
template: '',
templateUrl: '',
controller: '',
controllerAs: ''
//...
})
.otherwise(routeConfigObj);
<html ng-app="myApp">
<head>...</head>
<body>
<header>
<h1>My app</h1>
<ul>
<li><a href="#/">Home</a></li>
<li><a href="#/about">About</a></li>
</ul>
</header>
<div class="content">
<div ng-view></div>
</div>
</body>
</html>
angular
.module('myApp', ['ngRoute'])
.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/', {
template: '<h2>{{page}}</h2>',
controller: ['$scope', function($scope){
$scope.page = 'home';
}]
})
.when('/about', {
template: '<h2>{{page}}</h2>',
controller: ['$scope', function($scope){
$scope.page = 'about';
}]
})
.otherwise({redirectTo: '/'});
}]);
By Alexe Bogdan
Inversion of control paradigm and dependency injection pattern in Angular
Web Developer, JavaScript Lover, User Experience Professional, working in the web and mobile application development industry.