Angular 循环通过HTTP请求的响应
我正在努力解决这个问题,我正在发出请求,我正在接收一个对象,我无法迭代它,所以我不知道如何完成,这是我的代码: 我有一个叫做CountryCode的接口 我的服务文件如下所示:Angular 循环通过HTTP请求的响应,angular,http,Angular,Http,我正在努力解决这个问题,我正在发出请求,我正在接收一个对象,我无法迭代它,所以我不知道如何完成,这是我的代码: 我有一个叫做CountryCode的接口 我的服务文件如下所示: ```import { OperatorCode } from './../../models/catalogs/OperatorCode'; import { CountryCode } from '../../models/catalogs/CountryCode'; import { environment } f
```import { OperatorCode } from './../../models/catalogs/OperatorCode';
import { CountryCode } from '../../models/catalogs/CountryCode';
import { environment } from './../../../environments/environment';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class DatosInicialesService {
baseUrl = environment.baseUrl;
constructor(private http: HttpClient) {}
getCountryCode(): Observable<CountryCode[]> {
return this.http.get<CountryCode[]>(
`${this.baseUrl}/api/Catalog/CountryCode`
);
}```
import { CountryCode } from './../../models/catalogs/CountryCode';
import { DatosInicialesService } from './../../services/actualizacion-de-datos/datos-iniciales.service';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-datos-iniciales',
templateUrl: './datos-iniciales.component.html',
styleUrls: ['./datos-iniciales.component.scss'],
})
export class DatosInicialesComponent implements OnInit {
countryCode = [];
operatorCode = [];
constructor(
private router: Router,
private datosInicialesService: DatosInicialesService
) {}
onSubmit(event: any) {
event.preventDefault();
this.router.navigate(['actualizacion-de-datos/mas-sobre-ti']);
}
ngOnInit(): void {
this.datosInicialesService.getCountryCode().subscribe((data) => {
this.countryCode = data
console.log(data);
});
}```
and this is what it shows in the console:
{Data: Array(252), Status: {…}, Info: {…}}
Data: (252) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},
...
Info: {Datetime: "2021-02-24T20:23:07.3906084+00:00", AcceptedUser: true}
Status: {Code: 200, Message: "Ok"}
How can I loop through that response and get the name and the description of that Data response, I'm pretty new to web development so that's why this question might be mocked. I have tried in different ways to loop that object but I can't find the way to access to those properties I need.
Thanks in advanced if somene can help me with that
这是my CountryCode接口:从“../Status.base”导入{Status}; 从“../Info.base”导入{Info}
Data: {
idNationality: string;
code: string;
name: string;
description: string;
}[];
Status: Status;
Info: Info;
}`
你能展示你的国家代码界面吗?是的,当然。数据:{IDNational:string;代码:string;名称:string;描述:string;}[];状态:状态;信息:信息;}`它抛出错误,因为它检查变量的类型,并且您的接口没有数据属性。它会编译,因为它最终会编译。如果你想避免这个错误,只需在你的界面上添加数据你是什么意思?我正在使用这个接口的结构,因为它已经在othjer项目中使用过了,但在此之前使用过它的开发人员已经不再在这里工作了,它向我显示了一个错误。“CountryCode[]类型上不存在属性“Data”,请检查编辑的答案如果它解决了您的问题,您肯定存在格式错误的问题
ngOnInit(): void {
this.datosInicialesService.getCountryCode().subscribe((data) => {
this.countryCode = data
data[0].Data.forEach( (element) => {
console.log(element.name);
console.log(element.description);
});
});