I am totally confused about inject in Angular. I do not know where to use it and why. Is it only used with factory as described here?
myController.$inject = ['$scope','notify'];
Here notify
is the name of the factory.
Dependency Injection is a software design in which components are given their dependencies instead of hard coding them within the component. It relieves a component from locating the dependency and makes dependencies configurable.
Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.
How can we inject the service dependency into the controller C# Asp.net Core? ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container. The built-in container is represented by IServiceProvider implementation that supports constructor injection by default.
The Dependency Injection pattern is a particular implementation of Inversion of Control. Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).
That is one approach to support Dependency Injection after your code is minified (if you choose to minify).
When you declare a controller, the function takes parameters:
function ($scope, notify)
When you minify the code, your function will look like this:
function (a, b)
Since AngularJS uses the function parameter names to infer DI, your code will break because AngularJS doesn't know about a
or b
.
To solve this problem, they provided additional ways to declare controllers (or other services/factories/etc) for that matter:
For controllers, use the $inject
method - here you pass an array of literals that map to the parameters of your controller function. So, if you provide
['$scope', 'notify']
then the value of the first parameter to your function will be the a scope object associated with this controller and the second parameter will be the notify service.
When declaring new controllers, services, etc, you can use the array literal syntax. Here, you do something like this:
angular.module('myModule').controller('MyController', ['$scope', 'notify', function ($scope, notify) { ... }]);
The array as a parameter to the controller function maps the DI objects to your function parameters.
I prefer Option #2 when declaring controllers etc as it is easier to read/understand/cross-check since it is all in the same place.
To complement @mark answer, it is important to note that using the $inject method in the style of:
MyController.$inject = ['$scope', 'notify'];
allows you to add injection dependencies when building providers which are the only angular recipes that don't allow 'friendly' annotation style i.e.:
.controller('MyController', ['$scope', 'notify',...
dependencies to be declared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With