import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class StudentService {
getStudentsList(): any[]
{
var StudentsList = [
{Id:101, Name: "Adi", Email:"jc.adi101@gmail.com" },
{Id:102, Name: "Pavan", Email:"pavan@gmail.com"},
{Id:103, Name: "Nani", Email:"Nani@gmail.com" },
{Id:104, Name: "Madhan", Email:"Madhan@gmail.com" }
];
return StudentsList;
}
}
import { Component, OnInit } from '@angular/core';
import { StudentService } from '../student.service';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.css']
})
export class StudentComponent implements OnInit {
StudentsList = [];
constructor(private studentService: StudentService )
{
}
ngOnInit() {
this.StudentsList = this.studentService.getStudentsList();
}
}
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
<tr *ngFor="let student of StudentsList">
<td>
{{student.Id}}
</td>
<td>
{{student.Name}}
</td>
<td>
{{student.Email}}
</td>
</tr>
</table>
<h1>Service example in angular</h1>
<app-student></app-student>
Output: