无法通过angular2中的get方法处理api响应?
问题描述:
我一直在试图从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!
答
我不知道你想代码,但什么语言规则的角度你只需指定JSON返回结果给一个变量,它会保持完整的JSON对象,像这样:
$scope.data = [];
$http({url: url}).then(function (rs) {
console.log(rs);
$scope.data = rs.data;
});
+0
其angular2!多数民众赞成在! –
有这里没有角码,你不能在角度上使用常规的JavaScript类。应用程序模块,控制器和/或工厂在哪里? – johan
@johan可能这是TypeScript – Mikhail
sry guys!我刚刚发现角度和角度2是完全不同的!是的,它的打字稿! –