Dependency Injection

Download angularjs eBook

Syntax

  • myApp.controller('MyController', function($scope) { ... }); // non-minified code

  • myApp.controller('MyController', ['$scope', function($scope) { ... }]); //support minification

  • function MyController(){}

    MyController.$inject = ['$scope'];

    myApp.controller('MyController', MyController); //$inject annotation

  • $injector.get('injectable'); //dynamic/runtime injection

Remarks

Providers cannot be injected into run blocks.

Services or Values cannot be injected into config blocks.

Make sure to annotate your injections so your code will not break on minification.

Related Examples

Stats

221 Contributors: 6
Tuesday, February 21, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook