首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角8界面+ Rest响应

角8界面+ Rest响应
EN

Stack Overflow用户
提问于 2019-08-23 15:52:42
回答 3查看 75关注 0票数 0

如何在组件html中显示(resultData、correlationId、errors)?

  1. Rest Json答复:
代码语言:javascript
复制
{
"correlationId": null,
"errors": [],
"resultData": [
[
{
"ssrefitem": 710,
"subservice": "232-Telefone Móvel",
"srefitem": 386,
"formcode": "SCaetano",
"useqty": true,
"useprice": true,
"taxratetype": "O3",
"usesupplier": true,
"rfqforcesupplier": false,
"usetype": false,
"useunit": false
},
{
"ssrefitem": 711,
"subservice": "233-Internet",
"srefitem": 386,
"formcode": "SCaetano",
"useqty": true,
"useprice": true,
"taxratetype": "O3",
"usesupplier": true,
"rfqforcesupplier": false,
"usetype": false,
"useunit": false
}
]
],
"valid": true
}
  1. 接口,用于Json响应( servi.ts)。我使用这个https://app.quicktype.io进行“转换”。
代码语言:javascript
复制
 export interface SubServices {
  correlationId: null;
  errors: any[];
  resultData: Array<ResultDDD[]>;
  valid: boolean;
}

export interface ResultDDD {
  ssrefitem: number;
  subservice: string;
  srefitem: number;
  formcode: string;
  useqty: boolean;
  useprice: boolean;
  taxratetype: string;
  usesupplier: boolean;
  rfqforcesupplier: boolean;
  usetype: boolean;
  useunit: boolean;
}
  1. 我的服务(sot.service.ts)
代码语言:javascript
复制
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {SubServices} from './servi';


@Injectable({
  providedIn: 'root'
})
export class SotService {


  private sotUrl = 'cannot-give-the-real-one-but-the-response-is-in-the-1.';
  constructor(private http: HttpClient) { }

  getSubServices(): Observable<SubServices[]> {
    return this.http.get<SubServices[]>(this.sotUrl);
  }
}
  1. 我的组件(servi.component.ts)
代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import {SotService} from '../sot.service';
import { SubServices} from '../servi';


@Component({
  selector: 'app-sotlist',
  templateUrl: './sotlist.component.html',
  styleUrls: ['./sotlist.component.css']
})
export class SotlistComponent implements OnInit {

  todos: SubServices[];
  constructor( private sotService: SotService) {}

  ngOnInit() {
    this.getSubServices();
  }

   getSubServices() {
     this.sotService.getSubServices()
       .subscribe(
         (data: SubServices[]) =>  {
           this.todos = data;
           console.log(this.todos);
         }
       );
  }
}

如果我在component.html中使用这样的东西

代码语言:javascript
复制
<div>

  <h3>Something Something</h3>


  <ul class="items">
    <li *ngFor="let res of todos.resultData">
      {{res}}
    </li>
  </ul>
</div>

它会给我:

1.物体物体,物体物体

  1. 控制台中的resultData在ngFor中的用法是:"ERROR TypeError:无法读取未定义的属性'resultData‘“
EN

回答 3

Stack Overflow用户

发布于 2019-08-23 16:13:05

在您的响应中,resultData是一个数组。因此,您需要将下面的代码放在html中。

代码语言:javascript
复制
<ul class="items">
    <li *ngFor="let res of todos.resultData[0]">
      {{res}}
    </li>
</ul>

如果只放置{{res},它将在DOM中给出对象对象。如果您想显示整个对象,则需要给出类似于{res\ json }}的管道。

票数 1
EN

Stack Overflow用户

发布于 2019-08-23 16:05:27

在您的服务中,将http调用更改为:

代码语言:javascript
复制
return this.http.get<SubServices>(this.sotUrl); 

然后在component.ts中将getSubServices()方法更改为:

代码语言:javascript
复制
   getSubServices() {
     this.sotService.getSubServices()
       .subscribe(
         (data: SubServices) =>  {
           this.todos = data;
           console.log(this.todos);
         }
       );
  }

你的模板:

代码语言:javascript
复制
<div>

  <h3>Something Something</h3>


  <ul class="items">
    <li *ngFor="let res of todos.resultData">
      {{res[0].subservice}}
    </li>
  </ul>
</div>
票数 0
EN

Stack Overflow用户

发布于 2019-08-23 16:26:07

好的,我将组件更改为:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import {SotService} from '../sot.service';
import {ResultDDD, SubServices} from '../servi';


@Component({
  selector: 'app-sotlist',
  templateUrl: './sotlist.component.html',
  styleUrls: ['./sotlist.component.css']
})
export class SotlistComponent implements OnInit {

  todos: SubServices;
  public resul: ResultDDD[];
  constructor( private sotService: SotService) {}

  ngOnInit() {
    this.getSubServices();
  }

   getSubServices() {
     this.sotService.getSubServices()
       .subscribe(
         (data: SubServices) =>  {
           this.todos = data;
           this.resul = this.todos.resultData[0];
           console.log(this.todos);
         }
       );
  }
}

现在在我使用的html中:

代码语言:javascript
复制
<div>

  <h3>Something Something</h3>


  <ul class="items">
    <li *ngFor="let res of resul">
      {{res.ssrefitem}}
    </li>
  </ul>
</div>

没有控制台错误,它显示正确(并且在html端自动完成).Thks和Parth Dhankecha。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57629555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档