Last active
November 16, 2016 05:48
-
-
Save jrwebdev/b747f01b1a78f3ca376f89af894e1523 to your computer and use it in GitHub Desktop.
Angular 1 vs Angular 2 Component Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Angular 1 | |
| import NumberService from './path/to/number-service'; | |
| const module = angular.module('myModule', []); | |
| module.service('NumberService', () => NumberService); | |
| module.component('myComponent', { | |
| controller: ['NumberService', function (numberService) { | |
| this.value = numberService.random(1, 100); | |
| }, | |
| template: ` | |
| <div>{{$ctrl.value}}</div> | |
| ` | |
| }); | |
| /***************************************************************/ | |
| // Angular 2 | |
| import {Component, Output, EventEmitter} from '@angular/core'; | |
| import NumberService from './path/to/number-service'; | |
| @Component({ | |
| selector: 'my-component', | |
| providers: [ | |
| NumberService | |
| ], | |
| template: ` | |
| <div>{{value}}</div> | |
| ` | |
| }) | |
| class MyComponent { | |
| value: number; | |
| constructor (private numberService: NumberService) { | |
| this.value = numberService.randomNumber(1, 100); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment