从 *ngFor 迭代对象数组 - Angular
我的版本是 13.0.4
我只是尝试使用 ngFor 从 component.html 进行迭代,但它没有显示并且导致页面错误。 html:
<select>
<option *ngFor="let region of regiones" value="{{ region.numero }}"> {{ region.nombre }} </option>
</select>
我的界面:
export interface Region {
nombre: String,
numero: number
}
我正在获取服务中的区域(regiones.service.ts)
import { Injectable } from '@angular/core';
import { Region } from '../interfaces/regiones.interface';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class RegionesService {
constructor(private _http: HttpClient) { }
private _regiones: Region[] = [
{ nombre: 'Región Metropolitana de Santiago', numero: 0 },
{ nombre: 'Región de Arica y Parinacota', numero: 15 },
{ nombre: 'Región de Tarapacá', numero: 1 },
{ nombre: 'Región de Antofagasta', numero: 2 },
{ nombre: 'Región de Atacama', numero: 3 },
{ nombre: 'Región de Coquimbo', numero: 4 },
{ nombre: 'Región de Valparaíso', numero: 5 },
{ nombre: 'Región del Libertador General Bernardo O’Higgins.', numero: 6 },
{ nombre: 'Región del Maule', numero: 7 },
{ nombre: 'Región del Ñuble', numero: 16 },
{ nombre: 'Región del Biobío', numero: 8 },
{ nombre: 'Región de La Araucanía', numero: 9 },
{ nombre: 'Región de Los Ríos', numero: 14 },
{ nombre: 'Región de Los Lagos', numero: 10 },
{ nombre: 'Región de Aysén del General Carlos Ibáñez del Campo', numero: 11 },
{ nombre: 'Región de Magallanes y la Antártica Chilena', numero: 12 },
]
getRegiones() {
return { ...this._regiones };
}
}
然后我从组件中获取它们:(inicio.component.ts)
export class InicioComponent implements OnInit {
constructor(private regionesService: RegionesService) { }
regiones: Region[] = this.regionesService.getRegiones()
ngOnInit(): void {
console.log(this.regiones);
}
}
这是我在控制台中得到的:
结果:
错误:
core.mjs:6469 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.ngDoCheck (common.mjs:3156:27)
at callHook (core.mjs:2536:1)
at callHooks (core.mjs:2495:1)
at executeInitAndCheckHooks (core.mjs:2446:1)
at refreshView (core.mjs:9484:1)
at refreshComponent (core.mjs:10640:1)
at refreshChildComponents (core.mjs:9265:1)
at refreshView (core.mjs:9519:1)
at refreshEmbeddedViews (core.mjs:10594:1)
at refreshView (core.mjs:9493:1)
my version is 13.0.4
I'm just trying to iterate from component.html with ngFor but it doesn't show and it makes the page bug.
html:
<select>
<option *ngFor="let region of regiones" value="{{ region.numero }}"> {{ region.nombre }} </option>
</select>
My Interface:
export interface Region {
nombre: String,
numero: number
}
I'm getting the regions in a Service (regiones.service.ts)
import { Injectable } from '@angular/core';
import { Region } from '../interfaces/regiones.interface';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class RegionesService {
constructor(private _http: HttpClient) { }
private _regiones: Region[] = [
{ nombre: 'Región Metropolitana de Santiago', numero: 0 },
{ nombre: 'Región de Arica y Parinacota', numero: 15 },
{ nombre: 'Región de Tarapacá', numero: 1 },
{ nombre: 'Región de Antofagasta', numero: 2 },
{ nombre: 'Región de Atacama', numero: 3 },
{ nombre: 'Región de Coquimbo', numero: 4 },
{ nombre: 'Región de Valparaíso', numero: 5 },
{ nombre: 'Región del Libertador General Bernardo O’Higgins.', numero: 6 },
{ nombre: 'Región del Maule', numero: 7 },
{ nombre: 'Región del Ñuble', numero: 16 },
{ nombre: 'Región del Biobío', numero: 8 },
{ nombre: 'Región de La Araucanía', numero: 9 },
{ nombre: 'Región de Los Ríos', numero: 14 },
{ nombre: 'Región de Los Lagos', numero: 10 },
{ nombre: 'Región de Aysén del General Carlos Ibáñez del Campo', numero: 11 },
{ nombre: 'Región de Magallanes y la Antártica Chilena', numero: 12 },
]
getRegiones() {
return { ...this._regiones };
}
}
Then I get them from a component: (inicio.component.ts)
export class InicioComponent implements OnInit {
constructor(private regionesService: RegionesService) { }
regiones: Region[] = this.regionesService.getRegiones()
ngOnInit(): void {
console.log(this.regiones);
}
}
This is what I got in the console:
Result:
error:
core.mjs:6469 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.ngDoCheck (common.mjs:3156:27)
at callHook (core.mjs:2536:1)
at callHooks (core.mjs:2495:1)
at executeInitAndCheckHooks (core.mjs:2446:1)
at refreshView (core.mjs:9484:1)
at refreshComponent (core.mjs:10640:1)
at refreshChildComponents (core.mjs:9265:1)
at refreshView (core.mjs:9519:1)
at refreshEmbeddedViews (core.mjs:10594:1)
at refreshView (core.mjs:9493:1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在您的
getRegiones()
函数上:该函数的返回值是一个对象 - 而不是数组。这就是 Angular 抱怨无法循环的原因。
您可以使用
*ngFor="let Region of Object.values(regiones)"
将其转换回数组或更改getRegiones()
函数的结果。The problem is your
getRegiones()
function:The return value of this function is an object - not an array. This is why Angular complains about not being able to loop trough it.
You could use
*ngFor="let region of Object.values(regiones)"
instead to convert it back to an array or change the result of thegetRegiones()
function.