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!
Dependency Injection is pervasive throughout AngularJS. You can use it when defining components or when providing run and config blocks for a module.
The "Application Module" can be injected as a dependency 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.
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.
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'; }
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