无法使用.subscribe方法在本地存储

问题描述 投票:0回答:1

我的代码有问题(我可能误解了一些东西)。

我使用angular 5,这里是代码:

public readZWaveState(zwave_cmd:String):Observable<String>{     
  return this.http
       .get("http://ipAdd/core/api/jeeApi.php?apikey=xxx&type=cmd&id="+zwave_cmd)
       .map(res => res.json());}

我在另一个文件中订阅此方法,如下所示:

this.the4beesRestService.readZWaveState("xx").subscribe(
        res => {   
            this.l1_state = res;     
        });

这就是问题所在。我可以使用{{l1_state}}访问HTML代码中的值,但我无法在本地访问它。

我尝试使用console.log(this.l1_state),但它返回undefined。

我认为这是一个范围问题,所以如果有人可以帮助我,这将是伟大的。

javascript angular scope observable
1个回答
0
投票

使用以下代码尝试:

this.the4beesRestService.readZWaveState("xx").subscribe((res) => {   
            this.l1_state = res;     
            console.log(this.l1_state);
        });
© www.soinside.com 2019 - 2024. All rights reserved.