Service in Angular || Dependency Injection || Angular Fundamentals
In this video,I explained service in Angular. Feel free to comment. For more, please subscribe and press the BELL icon. Please visit my Angular Playlist. Please visit my YouTube Videos. The code is as follows -
courses.component.ts -
import { CoursesService } from './courses.service';
import { Component } from '@angular/core';
@Component ({
selector: 'courses',
template: `
<ul start=5>
<li *ngFor= "let course of courses">
{{ course }}
</li>
</ul>
`
})
export class CoursesComponent {
courses;
constructor(service: CoursesService) { //dependency injection
this.courses = service.getCourses();
}
}
courses.component.ts -
import { CoursesService } from './courses.service';
import { Component } from '@angular/core';
@Component ({
selector: 'courses',
template: `
<ul start=5>
<li *ngFor= "let course of courses">
{{ course }}
</li>
</ul>
`
})
export class CoursesComponent {
courses;
constructor(service: CoursesService) { //dependency injection
this.courses = service.getCourses();
}
}
courses.service.ts -
export class CoursesService {
getCourses() {
return ["HTML", "CSS","JS","PHP","AJS"];
}
}
Comments
Post a Comment