Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
Angular 角度4可观察值在日志中打印,但不传递到其他属性_Angular_Observable - Fatal编程技术网

Angular 角度4可观察值在日志中打印,但不传递到其他属性

Angular 角度4可观察值在日志中打印,但不传递到其他属性,angular,observable,Angular,Observable,我是个新手。。下面给出的是我正在编写的测试代码。。 小结:我的问题是,即使我能在日志中看到,我也无法将值返回到对象中。 细节:我使用Angular4,下面是我的组件代码、服务和html代码,我的意图是当我点击提交按钮时,订阅者将调用一个服务,使用“this.pythTicker”作为RESTAPI服务的参数,该服务将“this.pythTicker”的一些值返回到属性retPythService中。返回的值在日志中正确打印,但未分配给此.retPythService。所以我的问题是,我在推广这个

我是个新手。。下面给出的是我正在编写的测试代码。。 小结:我的问题是,即使我能在日志中看到,我也无法将值返回到对象中。 细节:我使用Angular4,下面是我的组件代码、服务和html代码,我的意图是当我点击提交按钮时,订阅者将调用一个服务,使用“this.pythTicker”作为RESTAPI服务的参数,该服务将“this.pythTicker”的一些值返回到属性retPythService中。返回的值在日志中正确打印,但未分配给此.retPythService。所以我的问题是,我在推广这个.retPythService的过程中是否做错了什么??我感到困惑的是,打印到上面的日志就可以了,但是订阅this.companyRet.compname就不行了 我最终的计划是使用属性“retPythService”填充HTML的第二行(this.companyRet.compname)

HTML#####pythsearch.component.HTML
<form (submit)="onPythSubmit()">
  <div class="form-group">
    <label>Name</label>
restApiSearch-Ticker: <input type="text" [(ngModel)]="pythTicker" name="Ticker"> <input type="submit">
  </div>
</form>
<h3> ticker details from a Pyth RESTAPI  {{ this.companyRet.compname }} </h3>
获取RESTAPI数据的服务

export class PythSrchService {
  PythApi= 'http://127.0.0.1:5002/PythSearch';
  retPythService: any;
  constructor( public http: Http )  {}
   getTickerDetails(PassTicker)  {
     this.retPythService = `${this.PythApi}\/${PassTicker} `;
     console.log('api call:', this.retPythService);
     // console.log( 'service call:' , this.http.get(this.retPythService).map(res => res.json()));
     return this.http.get(this.retPythService).map(res => res.json());
   }
日志输出

   api call: http://127.0.0.1:5002/PythSearch/TSLA 
    thisretpythservice in component : undefined
    compantret.compname:                   -->>> empty
    Object { bond: "", compname: "", convexity: "", price: "" } -->> empty
    retpthlog 
    Object { bond: "Testla Motor corp 10 year bond", compname: "tesla motor corp", convexity: "0.52", price: "102" } >>> Not empty , why?

如果其他新手遇到同样的小问题,发布答案
谢谢,到目前为止,我还不知道订阅和可观测的概念。。现在我明白了。下面的修复程序起作用了

// try 2 works now

this.PythSrchInst.getTickerDetails(this.pythTicker).subscribe(
  retPythService =>  {
    this.retPythService = retPythService ;
   // This was the problem
    this.companyRet = this.retPythService; 
    console.log('thisretpythservice in component :' , this.retPythService);
  });

您需要在回调中检查
this.retPythService
。当然,它在回调之外的下一行是不可访问的;如果是,那么回调就没有必要了。这就是给出回调和一般异步处理的全部要点。你可以在你自己的日志中看到这些事情发生的顺序。是的,顺序是错误的,当你指的是内部回调时,你指的是订阅()本身,对吗?在你传递给订阅的回调函数中,是的。谢谢,到目前为止,我还不知道订阅和可观察的概念。。现在我明白了。下面的修复程序有效..//try 2现在可以使用this.PythSrchInst.getTickerDetails(this.pythTicker).subscribe(retPythService=>{this.retPythService=retPythService;this.companyRet=this.retPythService;console.log('thisretpythservice in component:',this.retPythService);});然后接受副本,上面已经写了。
// try 2 works now

this.PythSrchInst.getTickerDetails(this.pythTicker).subscribe(
  retPythService =>  {
    this.retPythService = retPythService ;
   // This was the problem
    this.companyRet = this.retPythService; 
    console.log('thisretpythservice in component :' , this.retPythService);
  });