亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 我如何用異步和承諾初始化和填充數組

Angular 我如何用異步和承諾初始化和填充數組

胡說叔叔 2022-06-05 11:14:38
在我的組件和我的服務下方報告.component.html<div *ngFor="let report of reports">        <p>{{report.name}}</p>        <p>{{report.value}}</p>        <p>{{report.color}}</p></div>報告.component.tsexport class ReportComponent implements OnInit{reports = this.reportService.getLists('2020-03-20', '2020-03-26');constructor(private reportService: ReportService) {}ngOnInit(){}}報告服務.tsexport class ReportService {async getLists(startDate: string, endDate: string) {    this.getReport('2020-03-20', '2020-03-26').then(response => {      let lists = [    {        name: 'Opens',        value: response .opens, //2        color: 'green',      },        {        name: 'Closes',        value: response .opens, //1        color: 'red',      }];      return lists;    });  } async getReport(startDate: string, endDate: string) {    return await this.http      .get<Report>(       `https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`,        this.httpOptions      )      .toPromise();  } }可以初始化和填充數組列表嗎?我想在我的模板中使用 ngFor 循環,我不知道我是在組件還是服務中初始化數組。什么是最好的解決方案?更新export class ReportComponent implements OnInit {  reports: any;  constructor(private reportService: ReportService) { }  ngOnInit() {    // you still need to subscribe to obtain the result    this.reportService.getLists('2020-03-20', '2020-03-26').subscribe(        response => {            this.reports = response;             console.log(this.reports);      });  }}我測試了你的答案,但是它不起作用,我只是嘗試用值初始化我的數組,我的模板上有結果所以我認為,init數組不好..我只想使用報告對象從數組 List 初始化屬性值,例如 this.list[0].value = report.opens 我不知道它是否清楚....輸出(2) […]    0: {…}        name:"Opens"        value: 2        color:"green"        <prototype>: Object { … }    1: {…}        name:"Closes"        value: 1        color: "red"        <prototype>: Object { … }    length: 2    <prototype>: Array []在組件中的 console.log(this.report) 上方。
查看完整描述

2 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

您正在嘗試進行同步調用,而該調用又會發出異步請求。那不管用。當您在調用鏈中的某處進行異步調用時,您需要遵循它的規則。無論您嘗試如何,它都不會返回同步數據。


另一方面,您可以將異步調用的響應映射到您的格式數組。但是要檢索這個數組,您仍然需要遵守異步請求的規則。


嘗試以下


服務


export class ReportService {

  public getReport(startDate: string, endDate: string): Observable<any> {

    return this.http.get(`https://localhost:44354/report?startDate=${startDate}&endDate=${endDate}`, this.httpOptions)

      .pipe(map(response => {

        return 

        [

          { name: 'Opens', value: response.opens, color: 'green' },

          { name: 'Opens', value: response.opens, color: 'green' }

        ]

      }));

  }

}

控制器


import { Component, OnInit, ChangeDetectorRef } from '@angular/core';


export class ReportComponent implements OnInit {

  reports: any


  constructor(private reportService: ReportService, private _cdr: ChangeDetectorRef) { }


  ngOnInit() {

    // you still need to subscribe to obtain the result

    this.reportService.getLists('2020-03-20', '2020-03-26').subscribe(  

      response => { this.reports = response },

      error => { // handle error }

    );

    this._cdr.detectChanges();

  }

}

更新


您還可以在呈現數據之前進行檢查。


<ng-container *ngIf="reports">

  <div *ngFor="let report of reports">

    <p>{{report.name}}</p>

    <p>{{report.value}}</p>

    <p>{{report.color}}</p>

  </div>

</ng-container>


查看完整回答
反對 回復 2022-06-05
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

如果我使用它,我可以在日志中看到對象返回,但是為什么模板不適用于數組列表上的循環?


export class ReportComponent implements OnInit {

  reports: any


  constructor(private reportService: ReportService) { }


  ngOnInit() {

    // you still need to subscribe to obtain the result

    this.reportService.getLists('2020-03-20', '2020-03-26').subscribe(  

      response => { 

           this.reports = response;  

           console.log(this.reports);  

    });

  }

}


查看完整回答
反對 回復 2022-06-05
  • 2 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號