/I have created constantservice class/
import { Injectable } from ‘@angular/core’;
import {Http, Response} from ‘@angular/http’;
import ‘rxjs/Rx’;
@Injectable()
export class ConstantService {
data:any;
constructor(public http:Http){ };
getData() {
return this.http.get(“assets/data/datafolder.json”)
.map((res:Response) => res.json().YOUR_JSON_HEADER); //records in this case
}
load() {
console.log(‘json called’);
return new Promise(resolve => {
this.http.get(‘assets/data/datafolder.json’).map(response => {
this.data = response.json();
resolve(this.data);
});
});
}
}
in app.component.ts
import { ConstantService } from ‘…/service/constantservice’;
@Component({
templateUrl: ‘app.html’,
providers: [ConstantService]
})
export class MyApp {
myjsondata:any;
patdata:any;
constructor( public constantServiceData: ConstantService) {
this.constantServiceData.getData().subscribe((data1) => {
console.log("what is in the data ", data);
this.myjsondata = data1;
console.log(data1);
});
this.constantServiceData.load().then((data) => {
console.log("what is in the data ", data);
this.patdata= data;
console.log(data);
});
}
/**json data/
/datafolder.json in assets/data/
{
“records”: {
“patients”: {
“day”: “Today”,
“details”: [
{
“name”:“Diab”,
“stat”:“Pending”,
“phno”:“8197246465”,
“patNames”:“Sandra Adams”,
“tests”: [
{“name”:“MCHC”,“result”:“positive”}
]
}
]
}
}
}
bot cases data is showing undefinde
/app moule .ts/
import { HttpModule } from ‘@angular/http’;
imports:[HttpModule]
/**** is anything iam missing it , showing no errros too **/