小编Pau*_*tro的帖子

订阅Observable值

当我在订阅中记录我的变量的值时,我有一个值但是在外面做它时我得到未定义的???:

this._postService.getConf("app/conf/conf.json")
   .subscribe(res =>{
      this.home = JSON.stringify(res);
      console.log("value :" + this.home);
Run Code Online (Sandbox Code Playgroud)

我想用一定的值初始化我的home变量我在ngOnInit上做它但是当我试图把它弄到外面时它得到值undefined:

这是我的功能:

getConf(url) {
   return this._http.get(url).
      map(res => {return res.json()});
}

ngOnInit() {

  this._postService.getConf("app/conf/conf.json")
   .subscribe(res => {
        this.home = JSON.stringify(res);
        console.log("hahowaaaaaaaa" + this.home);
   }
  );

  this._postService.getPosts(this.home)
  .subscribe(result =>{ 
    this.loading = false;
    this.actionsG = result; 
    var count = JSON.stringify(result);

    this.aCount = count.substring(count.indexOf(','),23);

  },error=> console.error('Error: ' + error), ()=> console.log('finish! ActionsG'));

  this._postService.getPosts(this.home ).subscribe(result => this.actionsV = 
    result, error=> console.error('Error: ' + error), ()=> console.log('finish! ActionsV ngOnInit' + this.page));
}
Run Code Online (Sandbox Code Playgroud)

observable typescript angular

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

angular ×1

observable ×1

typescript ×1