Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2呼叫服务_Angular - Fatal编程技术网

Angular2呼叫服务

Angular2呼叫服务,angular,Angular,我刚刚开始学习angular2。我在angular2中调用服务时遇到问题。服务调用成功,但我在如何处理数据方面遇到问题。在Angular1中,我们这样做: DemoService.getExchangeDataConnection().then(function(response){ //here we handle all the data }) test() { return this.http.get('http://...').map(res => res.json());

我刚刚开始学习angular2。我在angular2中调用服务时遇到问题。服务调用成功,但我在如何处理数据方面遇到问题。在
Angular1
中,我们这样做:

DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
  return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
  (...)
});
角2

constructor(private _sampleService: SampleService){
    this._sampleService = _sampleService;
}


 onClickMe(){
    this.clickMessage ='You are my hero!';
     this.error = "";
    this.countries = [];
    this._sampleService.test()
     .subscribe(
        data => this.countries = data,
        error => this.error = "Region " + this.region + " is invalid."
     );

  }
这里我如何处理数据 这是我的服务:

export class SampleService {

constructor(http: Http){
       this.http = http;
   }

   test(){
console.log(AppSettings.API_ENDPOINT);
    return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
       .then(res => console.log(res.json()), err => console.log(err));
   }

}

如果
test
方法返回一个可观察到的,您需要以与承诺类似的方式订阅它:

this._sampleService.test()
   .subscribe(
     data => this.countries = data,
     error => this.error = "Region " + this.region + " is invalid."
   );
例如,对于这样的
测试
方法:

DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
  return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
  (...)
});
您可以注意到,您还可以使用Angular2的承诺。在本例中,您将以与之前相同的方式处理响应

编辑

您可以通过以下方式更新
测试方法:

test(){
  console.log(AppSettings.API_ENDPOINT);
  return this.http.get(AppSettings.API_ENDPOINT+"oceania")
               .map(res => console.log(res.json());
 }
this.service.test().then(data => {
  (...)
});
这样称呼它:

DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
  return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
  (...)
});
如果您想利用promise,您需要在
回调中返回一些内容以利用链接:

test(){
  console.log(AppSettings.API_ENDPOINT);
  return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
    .then(res => res.json(), err => console.log(err)); // <---------
}

如果
test
方法返回一个可观察到的,您需要以与承诺类似的方式订阅它:

this._sampleService.test()
   .subscribe(
     data => this.countries = data,
     error => this.error = "Region " + this.region + " is invalid."
   );
例如,对于这样的
测试
方法:

DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
  return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
  (...)
});
您可以注意到,您还可以使用Angular2的承诺。在本例中,您将以与之前相同的方式处理响应

编辑

您可以通过以下方式更新
测试方法:

test(){
  console.log(AppSettings.API_ENDPOINT);
  return this.http.get(AppSettings.API_ENDPOINT+"oceania")
               .map(res => console.log(res.json());
 }
this.service.test().then(data => {
  (...)
});
这样称呼它:

DemoService.getExchangeDataConnection().then(function(response){
//here we handle all the data
})
test() {
  return this.http.get('http://...').map(res => res.json());
}
this.service.test().subscribe(data => {
  (...)
});
如果您想利用promise,您需要在
回调中返回一些内容以利用链接:

test(){
  console.log(AppSettings.API_ENDPOINT);
  return this.http.get(AppSettings.API_ENDPOINT+"oceania").toPromise()
    .then(res => res.json(), err => console.log(err)); // <---------
}

I get subscribe不是一个函数
test
方法返回什么?它返回数据,但我不知道如何处理它可以在您的问题中添加此方法的内容吗?这将更容易帮助您…我得到subscribe不是一个函数,
测试
方法返回什么?这将返回数据,但我不知道如何处理此问题可以在您的问题中添加此方法的内容吗?帮助你会更容易…不要把你的http调用转换成承诺。不要把你的http调用转换成承诺。