Home / Web Development Technologies / AngularJS / AngularJS Dependency Injection

AngularJS Dependency Injection

AngularJS Dependency Injection

First Let’s Understand What is Dependency Injection in general:

angularjs dependency injection

You may have heard the word of ‘Dependency Injection’ before in PHP, Dependency injection or type hinting is the same idea and you might used it many times in Laravel PHP framework, anyways if you are not familiar with dependency injection, than no problem we are going to teach you from the ground,

From PHP point of view, Dependency Injection means to inject an object of the class to a function called Method injection or to a constructor called Constructor injection. than we can utilize the object of the injected class in that function.

Let Suppose we have two classes, Class A and Class B. as

class A{
public function abc(){}
public function abc2(){}

class B{
public function bca(A $obj){}

In the above code , we have two classes, Class A has two functions ‘abc()’ and ‘abc2()’. Class B has one function ‘bca()’ and the magic is here, We have injected Class A object to Class B function as shown above and we can utilize and access all public methods through that object.This was the concept of Dependency Injection.

Dependency Injection in AngularJS:

So, Dependency Injection in AngularJS is almost the same idea, we have builtin objects and when we need them, we can simply inject them to our controller constructor. let suppose we have a controller called ‘myController’. we are going to inject $scope object to it, here “$” doesn’t have any special meaning but it is used for naming convention.


var app = angular.module(“myApp”,[]);
app.controller(“myController”,function($scope) {
$scope.webUrl = “growthefuturenow.com”;

So, the object “$scope” will bind the data and automatically accessible to view, and we don’t have to create it by calling any function etc, We just type hint or inject it and all of its functionality is available. if we need any dependency in angularjs, we just have to inject it, beside $scope there is a lot of dependencies. If you need to bind data to the location URL in the user’s browser, we can inject $location object as:

app.controller(“myController”,function($scope, $location) {
// use $location here…

So, this magical effect through Angular’s dependency injection system. AngularJS Dependency
injection lets us follow a development style in which, instead of creating dependencies,
our classes just ask for what they need.

– For Advance usage and Further Reading Visit Angularjs Docs .

Having any query or suggestion related AngularJS Dependency injection, feedback us using below comment box.

Related Topics :

What is AngularJs

Promises In An Angular-Js

Angular-JS Templates and Data Binding

Angular-JS Lists Tables and Other Repeated Elements

Check Also

MongoDB Features and future

MongoDB Features and Future

MongoDB Features and Future. Welcome to the world of MongoDB. This article delivers a brief …

Non Relational Database or NoSql

Non Relational Database OR NoSQL

Non Relational  Database OR NoSQL Definition – What does Non Relational Database or NoSQL mean? …

Leave a Reply

Your email address will not be published. Required fields are marked *