Last active
October 28, 2021 05:12
-
-
Save jhades/a17be59e153a01e9f65869caa5a80e12 to your computer and use it in GitHub Desktop.
Angular Dependency Injection: Complete Guide - https://blog.angular-university.io/angular-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
export class CoursesService() { | |
http: HttpClient; | |
constructor() { | |
this.http = new HttpClient(... dependencies needed by HTTPClient ...); | |
} | |
... | |
} | |
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
@Injectable() | |
export class CoursesService() { | |
http: HttpClient; | |
constructor(http: HttpClient) { | |
this.http = http; | |
} | |
... | |
} | |
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
export class CoursesService() { | |
http: HttpClient; | |
constructor(http: HttpClient) { | |
this.http = http; | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'] | |
}) | |
export class CourseCardComponent { | |
constructor(private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
function coursesServiceProviderFactory(http:HttpClient): CoursesService { | |
return new CoursesService(http); | |
} | |
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
export const COURSES_SERVICE_TOKEN = | |
new InjectionToken<CoursesService>("COURSES_SERVICE_TOKEN"); | |
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
@NgModule({ | |
imports: [ | |
... | |
], | |
declarations: [ | |
... | |
], | |
providers: [ | |
{ | |
provide: COURSES_SERVICE_TOKEN, | |
useFactory: coursesServiceProviderFactory, | |
deps: [HttpClient] | |
} | |
] | |
}) | |
export class CoursesModule { } | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'] | |
}) | |
export class CourseCardComponent { | |
constructor( @Inject(COURSES_SERVICE_TOKEN) private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
@NgModule({ | |
imports: [ | |
... | |
], | |
declarations: [ | |
... | |
], | |
providers: [ | |
{ | |
provide: CoursesService, | |
useFactory: coursesServiceProviderFactory, | |
deps: [HttpClient] | |
} | |
] | |
}) | |
export class CoursesModule { } | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'] | |
}) | |
export class CourseCardComponent { | |
constructor( @Inject(CoursesService) private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
@NgModule({ | |
imports: [ | |
... | |
], | |
declarations: [ | |
... | |
], | |
providers: [ | |
{ | |
provide: CoursesService, | |
useClass: CoursesService, | |
deps: [HttpClient] | |
} | |
] | |
}) | |
export class CoursesModule { } | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'] | |
}) | |
export class CourseCardComponent { | |
constructor(private coursesService: CoursesService) { | |
... | |
} | |
... | |
} |
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
@NgModule({ | |
imports: [ | |
... | |
], | |
declarations: [ | |
... | |
], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class CoursesModule { } | |
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
@Injectable() | |
export class CoursesService() { | |
http: HttpClient; | |
constructor(http: HttpClient) { | |
this.http = http; | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'choose-quantity', | |
templateUrl: "choose-quantity.component.html", | |
styleUrls: ["choose-quantity.component.scss"], | |
providers: [ | |
{ | |
provide: NG_VALUE_ACCESSOR, | |
multi:true, | |
useExisting: ChooseQuantityComponent | |
} | |
] | |
}) | |
export class ChooseQuantityComponent implements ControlValueAccessor { | |
} | |
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
let counter = 0; | |
@Injectable() | |
export class CoursesService() { | |
constructor(private http: HttpClient) { | |
counter++; | |
this.id = counter; | |
} | |
... | |
} | |
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
<div class="courses"> | |
<course-card *ngFor="let course of courses " | |
[course]="course"> | |
<course-image [src]="course.iconUrl"></course-image> | |
</course-card> | |
</div> |
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
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class AppComponent { | |
constructor(private coursesService: CoursesService) { | |
console.log(`App component service Id = ${coursesService.id}`); | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class CourseCardComponent { | |
constructor(private coursesService: CoursesService) { | |
console.log(`course card service Id = ${coursesService.id}`); | |
} | |
... | |
} |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'], | |
providers: [] | |
}) | |
export class CourseCardComponent { | |
constructor(private coursesService: CoursesService) { | |
console.log(`course card service Id = ${coursesService.id}`); | |
} | |
... | |
} |
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
@NgModule({ | |
imports: [ | |
CommonModule | |
], | |
declarations: [ | |
CourseCardComponent | |
], | |
exports: [ | |
CourseCardComponent | |
], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class CoursesModule { } | |
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
@NgModule({ | |
declarations: [ | |
AppComponent | |
], | |
imports: [ | |
BrowserModule, | |
HttpClientModule, | |
CoursesModule | |
], | |
providers: [ | |
CoursesService | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule {} | |
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
@NgModule({ | |
imports: [ | |
CommonModule | |
], | |
declarations: [ | |
CourseCardComponent | |
], | |
exports: [ | |
CourseCardComponent | |
] | |
}) | |
export class CoursesModule { } | |
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
@Injectable({ | |
providedIn: CoursesModule | |
}) | |
export class CoursesService() { | |
constructor(private http: HttpClient) { | |
} | |
... | |
} | |
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
@Injectable({ | |
providedIn: "root" | |
}) | |
export class CoursesService() { | |
constructor(private http: HttpClient) { | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'] | |
}) | |
export class CourseCardComponent { | |
constructor(@Optional() private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class CourseCardComponent { | |
constructor(@SkipSelf() private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
@Component({ | |
selector: 'course-card', | |
templateUrl: './course-card.component.html', | |
styleUrls: ['./course-card.component.css'], | |
providers: [ | |
CoursesService | |
] | |
}) | |
export class CourseCardComponent { | |
constructor(@Self() private coursesService: CoursesService) { | |
... | |
} | |
... | |
} | |
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
@Directive({ | |
selector: '[highlighted]' | |
}) | |
export class HighlightedDirective { | |
constructor( @Host() private coursesService: CoursesService) { | |
console.log('coursesService highlighted ' + coursesService.id); | |
} | |
... | |
} | |
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
<div class="courses"> | |
<course-card *ngFor="let course of courses " highlighted | |
[course]="course"> | |
<course-image [src]="course.iconUrl"></course-image> | |
</course-card> | |
</div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment