angularjs 1.5 component dependency injection angularjs 1.5 component dependency injection angularjs angularjs

angularjs 1.5 component dependency injection


You can inject services to component controller like this:

angular.module('app.module')        .component('test', {            templateUrl: 'views/someview.html',            bindings: {                subject: '='            },            controller: ['$scope', 'AppConfig', TestController]        });    function TestController(scope, config) {        scope.something = 'abc';    }

or like this:

angular.module('app.module')        .component('test', {            templateUrl: 'views/someview.html',            bindings: {                subject: '='            },            controller: TestController        });    TestController.$inject = ['$scope', 'AppConfig']    function TestController(scope, config) {        scope.something = 'abc';    }


You should be able to inject services into your component's controller just like a standalone controller:

controller: function(Utils, authService) {    this.ms = 'tlOverallheader!'    authService.doAuthRelatedActivities().then(...);}


The accepted answer isn't minification safe. You can use the minification-safe dependency injection notation here too:

controller: ['Utils', 'authService',  function(Utils, authService) {    this.ms = 'tlOverallheader!'    authService.doAuthRelatedActivities().then(...);  },]