0
我一直在试图从api(http://www.cricapi.com/how-to-use.aspx)和angular2中获得响应。我不知道我做错了什么?这里是我的代码 -无法通过angular2中的get方法处理api响应?
//matchlist.component.ts
@Component({
selector: 'match-list',
template: `
<ul>
<li *ngFor="let m of matches">
<p>{{m.title}}</p>
</li>
</ul>
`,
providers: [MatchListService]
})
export class MatchList implements OnInit{
matches: Match[] = [];
object: any;
constructor(private matchservice: MatchListService){
}
ngOnInit(){
this.matchservice.getMatchList()
.subscribe(
matches => this.matches = matches,
err => {}
);
}
}
这里是我的服务接收机类
@Injectable()
export class MatchListService{
private matchListUrl = 'http://cricapi.com/api/cricket/';
constructor (private http: Http) {
}
getMatchList(): Observable<Match[]>{
return this.http.get(this.matchListUrl, [{}])
.map((res:any) => res.json()
.catch(this.handleError));
}
请告诉我,如果我做了错误的方式?这是我第一次使用api!
有这里没有角码,你不能在角度上使用常规的JavaScript类。应用程序模块,控制器和/或工厂在哪里? – johan
@johan可能这是TypeScript – Mikhail
sry guys!我刚刚发现角度和角度2是完全不同的!是的,它的打字稿! –