Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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 不与订阅一起工作-Angualr_Angular_Rxjs_Ngfor - Fatal编程技术网

Angular 不与订阅一起工作-Angualr

Angular 不与订阅一起工作-Angualr,angular,rxjs,ngfor,Angular,Rxjs,Ngfor,我想使用*ngFor将Api中的数据显示到div中,但没有起作用,我有点确定问题在于我用来存储Api数据的变量未定义,因为Api需要一些时间才能获得响应。我尝试了很多可能的解决方案,但没有一个奏效! 有什么帮助吗 这是我的服务文件 getFlightsData(from,to,goingDate):Observable<Iflight> { return this._http.get<Iflight>(this.url +'/' + from +'/' +to

我想使用
*ngFor
将Api中的数据显示到div中,但没有起作用,我有点确定问题在于我用来存储Api数据的变量未定义,因为Api需要一些时间才能获得响应。我尝试了很多可能的解决方案,但没有一个奏效! 有什么帮助吗


这是我的服务文件

getFlightsData(from,to,goingDate):Observable<Iflight> {
    return this._http.get<Iflight>(this.url +'/' + from +'/' +to +'/' + goingDate+'/' ,
      this.requestOptions)
    .pipe( 
      tap((data) => console.log(this._http + JSON.stringify(data['Quotes']))),
      catchError(this.handlerError)
    );
  }

有很多事情需要考虑

  • searchFlight()
    函数中的return语句没有执行您认为应该执行的操作。调用函数后,订阅将需要一些时间才能进入回调,因为它是异步的。但是
    返回false
    将立即触发

  • 类似地,即使在为变量
    flightsData
    赋值之前,也会立即调用
    load=false
    。这里可以使用
    finalize
    操作符

  • 要获得所有
    MinPrice
    属性的数组,可以使用
    array\map
    。它会给你类似于
    [466466466,…]
    的东西,即
    MinPrice
    值的数组

  • 控制器(*.ts)

    minPrices=[];
    
    searchFlight(from,to,goingDate,):void{//请也包括您的html和您得到的结果。您是否收到错误,或者它只是没有显示您期望的结果?hey@ShamPooSham刚刚添加了html,没有显示任何内容!NgFor之后的任何内容都没有显示console.log(this.flightsData)的结果?刚才也添加了这个!@sankasanjeewat要使用“ngFor”,this.flightsData应该是这样的数组[{“MinPrice”:466,…},{“MinPrice”:466,…},…]谢谢@MichaelD,但您的解决方案没有work@Omar:什么具体不起作用?是否收到任何错误消息或输出为空?
    searchFlight(from,to,goingDate,):boolean {
       this._skyScannerApiService.getFlightsData(from,to,goingDate,)
          .subscribe(
            (flightData) => { 
              this.flightsData = flightData['Quotes'];
              console.log(this.flightsData);
            },
            (error) => (this.errorMessage = <any>error)
          );
          this.loading = false;
          return false;
    
      }
    
    <div class="col-sm-3 " *ngFor="let flight of flightsData " [p]="flight">
                <div>
                    <label>
                    <span style="font-size: 100px; ">{{flight.MinPrice}}</span>
                    </label>
                </div>
    
                <div class="col-3 ">
    
                    <button class="btn btn-success " style="min-width: 100px; ">Fly</button>
    
                </div>
            </div>
    
     {
    "QuoteId": 1,
    "MinPrice": 466,
    "Direct": false,
    "OutboundLeg": {
    "CarrierIds": [
    1126
    ],
    "OriginId": 67764,
    "DestinationId": 47874,
    "DepartureDate": "2021-05-05T00:00:00"
    },
    "QuoteDateTime": "2021-05-02T12:06:00"
    }