Angular JS Dependency Injections


Dependency Injection is a design pattern in which components are provided with the list of dependencies i.e. libraries or services, instead of directly using them in the class.
How it works:
 When AngularJs application get bootstrapped it will check for angular module and its dependencies i.e. required libraries or services in the application needs to load in order to run the application.

AngularJS provides a supreme Dependency Injection mechanism. Below are the ways to use dependency injections:

Using dependency injection in Factory:
angular.module('Modulename', [])
.factory('servicename', ['depSername', function(depSername) {
  // code here
}])

Using dependency injection in Directive:
angular.module('Modulename', [])
.directive('directiveName', ['depSername', function(depSername) {
  // code here
}])

Using dependency injection in Filters:
angular.module('Modulename', [])
.filter('filterName', ['depSername', function(depSername) {
  // code here
}]);

Using dependency injection in Config method:
angular.module('Modulename', [])
.config(['depProvidername', function(depProvidername) {
  // code here
}])

Using dependency injection in run method:
angular.module('Modulename', [])
.run(['depServicename', function(depServicename) {
  // ...
}]);


Using dependency injection in controllers:
modulename.controller('CntrlName', ['$scope', 'depname1', 'depname2', function($scope, depname1, depname2) {
  $scope.methodname = function() {
    // code here
  }
}]);