Skip to content

Instantly share code, notes, and snippets.

@openopen114
Created May 10, 2017 06:52
Show Gist options
  • Save openopen114/96383ac2e47e8883af129a76fae7e47d to your computer and use it in GitHub Desktop.
Save openopen114/96383ac2e47e8883af129a76fae7e47d to your computer and use it in GitHub Desktop.
// create json file in assets folder
// in app.module.ts
import { HttpModule } from '@angular/http';
imports: [
BrowserModule,
FormsModule,
HttpModule
],
// in services.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class MyDataService {
constructor(private http: Http) { }
fetchData(){
this.http.get('./assets/data/student.json').map(
(response) => response.json()
).subscribe(
(data) => console.log(data)
);
}
obj = {
id:"1",
name:"open",
rollno:"2233"
}
success(){return "Successful" ;}
}
//in app.component.ts
import { Component } from '@angular/core';
import { MyDataService } from './my-data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
constructor(private newServices: MyDataService){}
ngOnInit(){
console.log(this.newServices.success());
console.log(this.newServices.obj);
this.newServices.fetchData();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment