samarthagarwal
9/24/2018 - 3:16 PM

IF_06_07_home.page.html

import { Component } from '@angular/core';
import { LoadingController } from '@ionic/angular';
import { HttpClient } from '@angular/common/http';

@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
})
export class HomePage {

    posts: any[] = [];

    constructor(private httpClient: HttpClient, private loadingController: LoadingController) {

        this.getPosts();

    }

    async getPosts() {
        let loading = await this.loadingController.create({
            message: "Loading Posts..."
        });

        await loading.present();

        // Making the GET request
        this.httpClient.get("https://jsonplaceholder.typicode.com/posts").subscribe((response) => {

            // Request succeeded
            console.log(response)
            this.posts = response as any[];

            loading.dismiss();

        }, (error) => {

            // Request failed with an error
            console.log(error);
            loading.dismiss();
        })

    }

}