Update Найдено решениемКак сделать прикованные запросы (различное число) с угловым 2
Я хочу, чтобы все страницы из Instagram API ({метки-имя} https://api.instagram.com/v1/tags//СМИ/недавнему? Access_token = Access- TOKEN) В ответе есть параметр «next_url», с помощью которого вы можете получить новую страницу. Я попробовал рекурсию, но это убивает браузер на ~ 13 шагах; Компонент:
import { Component, OnInit } from '@angular/core';
import {InstagramService} from './instagram.service';
import {Search} from '../search';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
posts: Post[];
tag: String = 'blabla23';
constructor(
private instagramService: InstagramService
) {}
ngOnInit() {
this.getPosts();
this.posts = [];
}
getPosts(nextUrl?: string): void {
this.instagramService
.getTagMedia(this.tag, nextUrl)
.then((result: Search) => {
this.posts = this.posts.concat(this.posts, result.getMedia());
if (result.getNextUrl() != null) {
this.getPosts(result.getNextUrl().toString());
}
});
}
}
Услуги:
import { Injectable } from '@angular/core';
import { Jsonp, Response, Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/delay';
import { Search } from './search';
@Injectable()
export class InstagramService {
private apiUrl = 'https://api.instagram.com/v1/';
private token = 'blalba';
constructor (private jsonp: Jsonp) {}
public getTagMedia(tag: String, url: string|null): Promise<Search> {
url = url || `${this.apiUrl}tags/${tag}/media/recent?access_token=${this.token}`;
url = `${url}&callback=JSONP_CALLBACK`
return this.jsonp.get(url)
.toPromise()
.then(response => { return new Search(response.json());})
}
}
Поиск модели:
export class Search {
private data;
private metadata;
private pagination;
constructor(row) {
this.data = row.data;
this.metadata = row.metadata;
this.pagination = row.pagination;
}
public getNextUrl(): string | null {
return this.pagination.next_url || null;
}
public getMedia() {
return this.data;
}
}
Я знаю о Observable.flatMap(), но не использовать его в этом случае из-за неизвестного количества запросов.
Как получить все страницы api? ps Я новичок в Angular 2 и js вообще. pps извините за мой английский