【问题标题】:How to return list inside subscribe Angular如何在订阅Angular中返回列表
【发布时间】:2022-09-22 21:11:57
【问题描述】:
winServiceInfo() {
    this.dataArrs=[]
    this.winServiceURL = JSON.parse(this.WinService[0].windowsServicesInfo)[\"Stactuscheck\"];
    this.service.getWinServicesInfo(this.winServiceURL)
    .pipe(
      catchError(this.handleError)
    )
    .subscribe((data: any) => {
        this.setSubscribeData(data);
        console.log(this.dataArrs)
    });
    console.log(this.dataArrs)
    return this.dataArrs;
}

setSubscribeData(data): any {
    this.WinService = data.windowsServicesInfo;
    this.dataArrs = this.getKeyValJsonObj();
return this.dataArrs;
}

第一个 console.log(this.dataArrs) 返回 Array(3),但第二个 console.log(this.dataArrs) 返回 Array(0)。我知道订阅是一个异步操作,因此。

那么如何处理从第二个 console.log(this.dataArrs) 返回 Array(3) 的情况

    标签: angular typescript angularjs-directive angular-ui-router angular-http


    【解决方案1】:

    而不是在函数内订阅,您需要返回 Observable

    this.dataArrs = winServiceInfo() {
      this.winServiceURL = 
      JSON.parse(this.WinService[0].windowsServicesInfo)["Stactuscheck"];
      return this.service.getWinServicesInfo(this.winServiceURL)
         .pipe(
            catchError(this.handleError)
              );
    }
    

    并在您的模板中:

    <tr *ngFor="let opc of dataArrs | async"> <tr>
    

    但是调用这个函数可以subscribe来获取数据

    【讨论】:

    • 如果适用,或者使用async 管道。
    • 仍然面临同样的问题 winServiceInfo(j: number) { this.winServiceURL = SON.parse(this.WinService[j].windowsServicesInfo["Stactuscheck"]; return this.service.getWinServicesInfo(this.winServiceURL) .pipe( catchError( this.handleError) ) } setSubscribeData(j: number): any { this.winServiceInfo(j).subscribe((data: any) => { this.WinService = data.windowsServicesInfo; this.dataArrs = this.getKeyValJsonObj(); console.log(this.dataArr) return this.dataArrs; }) console.log(this.dataArr) };
    • @Eli Porush 仍然面临同样的问题
    • 你能分享你显示这些数据的方式吗?
    • @Eli Porush 我想在 html 页面上调用 winServiceInfo() 以获取列表数据 <tr *ngFor="let opc of this.winServiceInfo(j);index as i"> <tr> 得到错误 core.js:4002错误类型错误:无法读取未定义的属性(读取“windowsServicesInfo”)打字稿代码
    【解决方案2】:

    @Eli Porush 在 html 页面中调用 winServiceInfo() 的原因是因为每个 url 都有 dataArr 列表。所以为什么我想要订阅之外的值 dataArr

    请找到代码sn-p作为参考,每个dataArr因不同而不同this.winServiceURL

    你的宝贵是非常值得赞赏的

    模板

    <div class="row" *ngFor="let otc of this.jsonData;index as j">
            <div>
                <table class="table table-striped table-fixed">
                 
        <tr *ngFor="let opc of this.winServiceInfo(j);index as i">
     
    

    打字稿

     winServiceInfo(j: number) {
        this.winServiceURL = JSON.parse(this.WinService[j].windowsServicesInfo)["Stactuscheck"];
        this.dataArrs = [];
        this.service.getWinServicesInfo(this.winServiceURL)
          .pipe(
            catchError(this.handleError)
          )
          .subscribe(
            (data: any) => {
              this.setSubscribeData(data);
              console.log(this.dataArrs);
              return this.dataArrs;
            });
        console.log(this.dataArrs);
        return this.dataArrs;
      }
    
    setSubscribeData(data): any {
        this.WinService = data.windowsServicesInfo;
        this.dataArrs = this.getKeyValJsonObj();
        return this.dataArrs;
      }
    
    getKeyValJsonObj() {
        this.dataArr = [];
        for (let key of this.sliceIntoChunks()) {
          for (let i in key) {
            this.dataArr.push({ 'key': i, 'value': key[i] });
          }
        }
        return this.dataArr;
      }
    

    错误错误类型错误:无法读取未定义的属性(读取“windowsServicesInfo”)

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2021-07-16
      • 2018-09-01
      • 1970-01-01
      • 2017-03-26
      • 2021-04-27
      相关资源
      最近更新 更多