Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
Javascript Angular2-将http.get响应传递给类对象_Javascript_Angular_Typescript - Fatal编程技术网

Javascript Angular2-将http.get响应传递给类对象

Javascript Angular2-将http.get响应传递给类对象,javascript,angular,typescript,Javascript,Angular,Typescript,我是个新手。我有一个json文件,可以在其中配置我需要在应用程序中使用的url app/config/development.json { "apiUrl": "http://staging.domain.com:9000/", "debugging": true } 下面是我在config.service.ts中的代码: export class ConfigService { private apiURL:any; constructor (private ht

我是个新手。我有一个json文件,可以在其中配置我需要在应用程序中使用的url

app/config/development.json

{
   "apiUrl": "http://staging.domain.com:9000/",
   "debugging": true
}
下面是我在config.service.ts中的代码:

export class ConfigService {
    private apiURL:any;
    constructor (private http: Http) {}

   getApiURL(){
       this.http.get("app/config/development.json").map(res:Response=>res.json())
      .subscribe(data=>{
         this.apiURL = data;

       })

        console.log(this.apiURL);//this returns undefined
   }

}
我想创建
this.apiURL
,以包含
http.get
的响应。 当我创建另一个方法时,
this.apiURL
的值仍然与方法
getAPIURL()
的值相同


你可以这样做。 在您的服务文件中

//whatever model u defined
       getApiURL():Observable<Object[]>{     
      return this.http.get(this.whateverURL)
                .map(res:Response=>res.json())
                .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }

Hi@echonax可能重复。谢谢你的评论,但这不是我想要的答案。我正在angular2中开发,它不同于您发布的链接中提到的jQuery ajax调用。更好的做法是服务应始终返回可观察/承诺&您需要在组件类中实现/解决这一点。请参考Hi@Parth,你能给我一个使用可观察和承诺的示例答案吗?谢谢Parth。我现在明白了。
//whatever model u defined
       getApiURL():Observable<Object[]>{     
      return this.http.get(this.whateverURL)
                .map(res:Response=>res.json())
                .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }
  yourData:Object[];
  //whatever Model u defined.
  //assuming yourService is your service instance which u did in constructor.
  this.yourService.getApiURL()
        .subscribe(
            yourData=>{
              this.yourData=yourData;               
            },err=>{
              console.log(err);
              alert("Something went wrong");
            }
        )
  }