现在,我有一个初始页面,其中有三个链接.点击最后的"朋友"链接后,会启动相应的朋友组件.在那里,我想获取/获取我的朋友的列表进入friends.json文件.直到现在一切正常.但我仍然是使用RxJs的observables,map,subscribe concept的angular2的HTTP服务的新手.我试图理解它并阅读一些文章,但在我开始实际工作之前,我不会理解这些概念.
在这里,我已经制作了除HTTP相关工作之外的plnkr.
myfriends.ts
import {Component,View,CORE_DIRECTIVES} from 'angular2/core';
import {Http, Response,HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/Rx';
@Component({
template: `
<h1>My Friends</h1>
<ul>
<li *ngFor="#frnd of result">
{{frnd.name}} is {{frnd.age}} years old.
</li>
</ul>
`,
directive:[CORE_DIRECTIVES]
})
export class FriendsList{
result:Array<Object>;
constructor(http: Http) {
console.log("Friends are being called");
// below code is new for me. So please show me correct way how to do it and please explain about .map and .subscribe functions and observable pattern.
this.result = http.get('friends.json')
.map(response => …Run Code Online (Sandbox Code Playgroud)