我對AngularJS很新。我正在使用Angular2製作一個Flickr公共提要應用程序,但我遇到了這個問題。Angular2 Flickr JSONP
我找不到讓JSONP工作的方法。我該怎麼做才能返回JSON對象?
這裏的代碼
flickr.service.ts
import { Injectable } from '@angular/core';
import { Jsonp, Http } from '@angular/http'; //use jsonp module to fetch the data from the api call
import 'rxjs/Rx'; // use rxjs as the observable
@Injectable()
export class FlickrService {
url: string;
apiKey: string = 'ffed2ef9dede27481c3195eea1be61eb';
constructor(private _jsonp: Jsonp) {
this.url = `https://api.flickr.com/services/feeds/photos_public.gne?&api_key=${this.apiKey}&per_page=12&format=json&nojsoncallback=1`;
}
getFeed() {
return this._jsonp.get(this.url)
.map(res => res.json());
}
searchTag(searchTerm: string) {
return this._jsonp.get(this.url+'&tag='+searchTerm)
.map(res => res.json());
}
}
flickr.component.ts
import { Component } from '@angular/core';
import { FlickrService } from '../services/flickr.service';
@Component({
moduleId: module.id,
selector: 'flickr',
templateUrl: './flickr.component.html'
})
export class FlickrComponent {
feedList: Array<Object>;
searchList: Array<Object>;
searchTerm: string;
constructor(private _flickrService: FlickrService) {
this._flickrService.getFeed().subscribe(res => {
console.log(res);
});
}
}
你可以添加ngmodule代碼嗎? – CharanRoot
你知道如何解決這個問題嗎?我面臨同樣的問題。 –