【发布时间】:2016-07-15 10:04:56
【问题描述】:
我一直在尝试使用 angular2 从 api (http://www.cricapi.com/how-to-use.aspx) 获得响应。我不知道我做错了什么?这是我的代码--
//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!
【问题讨论】:
-
这里没有 Angular 代码,你不能在 Angular 中使用常规的 javascript 类。应用模块、控制器和/或工厂在哪里?
-
@johan 这可能是 TypeScript
-
sry 伙计们!我刚刚发现 angular 和 angular2 完全不同!是的,它的打字稿!
-
也是我的错,我确定问题被标记为 AngularJS not 2...