Angular 2/4:获取订阅服务HTTP GET的价值

时间:2017-08-16 11:50:49

标签: angular typescript

当我尝试使用http get services获取价值时,我遇到了问题..

services.ts:

  getLabelTByCode(code: string) {
    return this.http.get('/administration/typeByCode/'+code+'')
      .map((response :Response) => {
        return response.json();
      })
  }

component.ts

name : string;
getTypeByCode(code : string) {
    return this.adminService.getLabelTByCode(code)
      .subscribe(
        label => { 
          this.labelTypeOfDemand = label;
          // Here I can get the value of the name
          return this.labelTypeOfDemand.name;  
        }
      );
  }

saveFolder() { // When I submit my fomrs, I called this method
   this.name = this.getTypeByCode('XER');
   console.log(this.name); // ==> I got undefined
}

为什么在方法saveFolder中未定义name的值?我怎样才能获得价值?

我的JSON喜欢:

{
      "id": 9,
      "code": "XER",
      "name": "TEST",
      "status": null
   },
      {
      "id": 7,
      "code": "PBA",
      "name": "TEST",
      "status": null
   },

0 个答案:

没有答案