1)app.component.html
<button (click)="load();">Load</button>
<table>
<tr><td>Id</td><td>Name</td></tr>
<tr *ngFor="let student of students">
<td>{{student.id}}</td>
<td>{{student.name}}</td>
</tr>
</table>
2)app.component.ts
import { Component, OnInit } from '@angular/core';
import { Student } from "./student";
import { StudentService } from "./student.service";
@Component({
selector: 'project',
templateUrl: 'app.component.html'
})
export class AppComponent {
students:Student[] ;
constructor(private studentService:StudentService){
this.students = [];
}
load(){
this.studentService.getStudents().subscribe(
(data)=>this.students=data
);
}
}
3)app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from "@angular/platform-browser";
import { AppComponent } from './app.component';
import { StudentService } from "./student.service";
import { HttpModule } from "@angular/http";
@NgModule({
imports: [BrowserModule,HttpModule],
declarations: [AppComponent],
providers:[StudentService],
bootstrap: [AppComponent],
})
export class AppModule { }
4)student.service.ts
import { Injectable } from '@angular/core';
import { Student } from "./student";
import { Http } from "@angular/http";
import { Observable } from "rxjs";
import 'rxjs/add/operator/map';
@Injectable()
export class StudentService {
constructor(private http:Http) { }
getStudents(){
return this.http.get('http://localhost:3000/students').map(
(response) => response.json()
);
}
}
5)student.ts
export class Student{
id:number;
name:string;
constructor(a,b){
this.id=a;
this.name=b;
}
}