here is my code for obtaining the data in angular, I am trying to make it more friendly for users and it takes a while to load, I am looking for a way to add a loader in angular I am new to angular.
I am using ionic cli to build a mobile app feed by json data
import { Component, OnInit } from '@angular/core';
import { DataService } from "../data.service";
@Component({
selector: 'app-articlespage',
templateUrl: './articlespage.page.html',
styleUrls: ['./articlespage.page.scss'],
})
export class ArticlespagePage implements OnInit {
//id : any[];
title : any[];
author : any[];
date : any[];
content : any[];
constructor(private dataService: DataService) { }
ngOnInit() {
this.dataService.getRemoteData().subscribe(data => {
console.log("Remote Data:" + JSON.stringify(data));
//console.log(data);
this.parseJson(data);
});
}
parseJson(data)
{
let jsonArray = data;
//this.id = [];
this.title = [];
this.author = [];
this.date = [];
this.content = [];
for(let i=0; i< jsonArray.length ; i++)
{
let jsonObject = jsonArray[i];
//this.id.push(jsonObject.id);
this.title.push(jsonObject.title);
this.author.push(jsonObject.author.name);
this.date.push(jsonObject.created_at );
this.content.push(jsonObject.blog_entry );
}
}
}
html:
<ion-content fullscreen>
<h2 > Articles </h2>
<ion-card class="feedspotcard-1 box" *ngFor=" let item of title; let i = index;">
<ion-card-content>
<div class="box-shadow">
<div class="coolbar"></div>
<!-- <p> <span> Id : </span> <span innerHTML={{id[i]}}> </span> </p> -->
<h4> <span innerHTML={{title[i]}}> </span> </h4>
<p> <span innerHTML={{content[i]}}> </span> </p>
<p> <span> author : </span> <span innerHTML={{author[i]}}> </span> </p>
<p> <span> date : </span> <span innerHTML={{date[i]}}> </span> </p>
</div>
</ion-card-content>
</ion-card>
</ion-content>
not necessary but result:
you need a property that keeps track of the state of the request. i.e. loadingData: boolean
.
in your component you would need to set this value at the start of the request, and when it completes.
ngOnInit(): void {
this.loading = true;
this.dataService.getRemoteData().subscribe(data => {
// set the status of loading to false so the template can update
this.loading =f alse;
console.log("Remote Data:" + JSON.stringify(data));
//console.log(data);
this.parseJson(data);
});
}
finally, in the template, add an element that is shown based on the value of the loading
property.
<div *ngIf="loading"><!-- Insert your progress message/animation --> </div>