Hi there,
I get a CORS error on the API service. To overcome this problem, I need to use the Native HTTP plug-in. I don’t know how to use native HTTP. My codes are below. And I don’t know how to use features like infinite scrolling with Native HTTP.
Can someone help me?
Thanks.
api.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class LaunchService {
public apiUrl: any = 'https://xxxxxx.com/api/3.3.0/';
currentArticle: any;
constructor(private http: HttpClient) { }
getApi(offset) {
console.log('next: ', offset);
const url = this.apiUrl + 'upcoming/?offset=' + offset + '&mode=detailed';
return this.http.get(url);
}
}
tab2.page.ts
import { HttpClient } from '@angular/common/http';
import { ApiService } from './../launch.service';
import { Component } from '@angular/core';
import { LoadingController } from '@ionic/angular';
@Component({
selector: 'app-tab2',
templateUrl: 'tab2.page.html',
styleUrls: ['tab2.page.scss']
})
export class Tab2Page {
data: any = [];
offset = 0;
constructor(private launchService: ApiService, private http: HttpClient, public loadingController: LoadingController) {}
ngOnInit() {
this.apiService
.getApi(this.offset)
.subscribe(data =>{
console.log(data);
this.data = data;
});
}
//infinity scroll start
LoadMore(event){
this.offset += 20;
console.log(event);
this.apiService
.getApi(this.offset)
.subscribe(data =>{
for(const article of data['results']){
this.data.results.push(article);
}
event.target.complete();
console.log(this.data);
});
}
//infinity scroll end
}