gpt4 book ai didi

angular - 循环浏览来自 HTTP 请求 Angular 的响应

转载 作者:行者123 更新时间:2023-12-04 08:02:58 25 4
gpt4 key购买 nike

我正在为此苦苦挣扎,我正在发出请求并且我正在接收一个对象,我无法迭代它,所以我不知道如何完成,这是我的代码:
我有一个名为 CountryCode 的接口(interface)
我的服务文件如下所示:

```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

最佳答案

ngOnInit(): void {
this.datosInicialesService.getCountryCode().subscribe((data) => {
this.countryCode = data
data[0].Data.forEach( (element) => {
console.log(element.name);
console.log(element.description);
});
});

关于angular - 循环浏览来自 HTTP 请求 Angular 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66358446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com