Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs 1.5 component dependency injection

this may sound newb, but I have been following this tutorial for angularjs component.

I am new to components and how do I inject a constant Utils or authService to my component like this?

app.component('tlOverallHeader', {     bindings: {         data: '='     },     templateUrl: 'js/indexTimeline/components/tl_overallHeader/templates/tl_overallHeader.html',     controller: function() {         this.ms = 'tlOverallheader!'     } }) 

thanks!

like image 350
Hokutosei Avatar asked Jan 20 '16 04:01

Hokutosei


People also ask

Does AngularJS support dependency injection?

Dependency Injection is pervasive throughout AngularJS. You can use it when defining components or when providing run and config blocks for a module.

Which component can be injected as dependency in AngularJS?

The "Application Module" can be injected as a dependency in AngularJS.

How do I inject a module in AngularJS?

Injecting a value into an AngularJS controller function is done simply by adding a parameter with the same name as the value (the first parameter passed to the value() function when the value is defined). Here is an example: var myModule = angular. module("myModule", []); myModule.

How does dependency injection work in AngularJS?

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. It also helps in making components reusable, maintainable and testable.


1 Answers

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';     } 
like image 116
Gondy Avatar answered Oct 12 '22 00:10

Gondy