Thursday, January 4, 2018

How to retrive data from json using angular2&4&5

Hey all,

Below is the component class for angular 2 and  get the data from JSON using like http service.


Create a component class

import { Component, OnInit } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map'

@Component({
    moduleId: module.id,
    selector: 'my-app',
    templateUrl: 'app.component.html'
})

export class AppComponent implements OnInit {

pagedItems: any[];

this.http.get('./dummy-data.json')
            .map((response: Response) => response.json())
            .subscribe(data => {
                // set items to json response
                this.allItems = data;

            });
prevPage(){
            console.log( this.allItems);
   }

}

No comments:

Post a Comment