Take one json file: assets/employeedetail.json file
[{
“EmpName”:”Monvi”,
“EmpId”:”12″,
“Skillset”:”Angular”
},
{
“EmpName”:”Rachana”,
“EmpId”:”13″,
“Skillset”:”Angular”
},
{
“EmpName”:”ram”,
“EmpId”:”12″,
“Skillset”:”Angular”
}]
app/httpservice.service.ts :
import { Injectable } from ‘@angular/core’;
import {Http, Response} from ‘@angular/http’;
import ‘rxjs/add/operator/map’;
@Injectable()
export class HttpserviceService {
constructor(private http:Http) { }
private url:string=”./assets/employeedetails.json”;
getWebDetails() {
return this.http.get(this.url)
.map((response:Response)=>response.json());
}
}
webcomponent.component.ts:
import { Component, OnInit } from ‘@angular/core’;
import { HttpserviceService } from ‘../httpservice.service’;
@Component({
selector: ‘app-webcomponent’,
templateUrl: ‘./webcomponent.component.html’,
styleUrls: [‘./webcomponent.component.css’]
})
export class WebcomponentComponent implements OnInit {
constructor(private http:HttpserviceService) { }
webdetails=[];
ngOnInit() {
this.http.getWebDetails()
.subscribe(result=>this.webdetails=result);
}
}
webcomponent.component.html
Web Data From Http
- {{uobj.EmpName}} {{uobj.EmpId}} {{uobj.Skillset}}
Web Data From Http
- {{uobj.EmpName}} {{uobj.EmpId}} {{uobj.Skillset}}
include app.module.ts file
import { BrowserModule } from ‘@angular/platform-browser’;
import { NgModule } from ‘@angular/core’;
import { HttpModule} from ‘@angular/http’;
import { AppComponent } from ‘./app.component’;
import { WebcomponentComponent } from ‘./webcomponent/webcomponent.component’;
@NgModule({
declarations: [
AppComponent,
WebcomponentComponent
],
imports: [
BrowserModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }