Implementing Dependency Injection

Once you have defined a module and appropriate providers, you can add the module as a dependency to other modules, controllers, and a variety of other AngularJS objects. You need to set the value of the $inject property of the object that depends on the providers. The $inject property contains an array of provider names that should be injected into it.

For example, the following code defines a basic controller that accepts the $scope and appMsg parameters. Then the $inject property is set to an array that contains $scope, which is the AngularJS scope service that provides access to the scope and a custom appMsg. Both $scope and appMsg are injected into the myController function:

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.