angular 2 rc4 在从 Http observable 获取数据后创建变量

angular 2 rc4 creating variable after getting data from Http observable

我是 angular2 的新手,我希望你能帮助我,我很确定它一定很简单,但这是交易,我正在制作一个可以处理 http 调用的服务,但它需要在数据是之后应用一些过滤器加载时没有再次询问 api 使用新参数的服务器基本上我想让 angular2 处理那部分

更新**

Service

httpHandler(type,model){var token = this.LoginService.JwtDecoded(); var authHeader = new Headers(); authHeader.append('Authorization', 'Bearer ' + token);

 return this.http.get('http://127.0.0.1/api/service/'+model, {
      headers: authHeader
      })
  .map((res: Response) => res.json());    }

Component

this.Request.httpHandler('get','receiver/')
.subscribe(
    data => this.data = data, <-- from here
    err => this.error = 'there is a mistake!',
    () => console.log(this.data) <-- this one is outputing just what i want
  );
console.log(data); <-- i wanna output the same result in here

那么,如何将 api 结果捕获到变量中?

更新** 我想在 subscribe 之外输出 'this.data',奇怪的是我也可以在 subscribe 和 html 中输出,但是我需要在 json 中输出之前用 json 做一些事情26=].. 伙计们有什么想法吗?谢谢!

谢谢你们抽出时间,非常感谢。

创建一个服务来获取数据

// xyz.service.ts

import {Injectable} from '@angular/core';

import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {
  private url = `receiver/`;

  constructor(private http: Http) {
  }

  getData(): Observable<any> {
    return this.http.get(this.url)
      .map((res: Response) => res.json());
  }
}

现在在组件中使用该服务。

//xyz.component.ts

import {Component, OnInit} from '@angular/core';

import {DataService} from './xyz.service';

import {HTTP_PROVIDERS} from '@angular/http';

@Component({
  selector: 'app-home-content',
  templateUrl: 'xyz.component.html',
  styleUrls: ['xyz.component.scss'],
  providers: [DataService, HTTP_PROVIDERS]
})
export class HomeContentComponent implements OnInit {
  private data: Object;

  constructor(private _dataService: DataService) {
  }

  ngOnInit() {
    this._dataService.getData().subscribe(data => {
      this.data = data;
    });
  }
}

现在数据将在 this.data

中可用