尝试区分“[object Object]”时出错。只允许数组和迭代 - Angular8
Error trying to diff '[object Object]'. Only arrays and iterables are allowed - Angular8
此 Angular 代码不允许我将 JSON 文件中的数组内容显示到 HTML 上。控制台中的错误显示 "Error trying to diff '[object Object]'"
这段代码在我的本地 运行 运行 Angular 8. 我尝试了很多不同的获取数据的方法和不同的显示方式 HTML但是 none 似乎有效
JSON File
{...} represent a string
{
"APPLICATIONS": [
{
"APP_ID": 1,
"APP_NAME": "...",
"APP_SHORT_NAME": "...",
},...
TS
export class Application {
APP_ID: number;
APP_NAME: string;
APP_SHORT_NAME: string;
}
Service.TS
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Application } from '../intdets';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IntdetsService {
private _url = "/assets/IntDet.json";
constructor(private http: HttpClient) { }
getIntdets(): Observable<Application[]> {
return this.http.get<Application[]>(this._url);
}
}
Component.TS
import { Component, OnInit } from '@angular/core';
import { IntdetsService } from '../service/intdets.service';
@Component({
selector: 'app-intdets',
templateUrl: './intdets.component.html',
styleUrls: ['./intdets.component.css']
})
export class IntdetsComponent implements OnInit {
public apps = [];
constructor(private _intdetsService: IntdetsService) { }
ngOnInit() {
this._intdetsService.getIntdets()
.subscribe(data => this.apps = data);
}
}
HTML
<ul *ngFor = "let app of apps">
<li>{{app.APP_ID}}</li> //Test code to only print one element
</ul>
据我所知,从服务返回的数据不是数组,它是一个包含 属性 数组的对象。所以 this.apps = { APPLICATIONS: [] }
如果你想迭代数组,你需要像这样访问 APPLICATIONS:
<ul *ngFor = "let app of apps.APPLICATIONS">
<li>{{app.APP_ID}}</li> //Test code to only print one element
</ul>
此 Angular 代码不允许我将 JSON 文件中的数组内容显示到 HTML 上。控制台中的错误显示 "Error trying to diff '[object Object]'"
这段代码在我的本地 运行 运行 Angular 8. 我尝试了很多不同的获取数据的方法和不同的显示方式 HTML但是 none 似乎有效
JSON File
{...} represent a string
{
"APPLICATIONS": [
{
"APP_ID": 1,
"APP_NAME": "...",
"APP_SHORT_NAME": "...",
},...
TS
export class Application {
APP_ID: number;
APP_NAME: string;
APP_SHORT_NAME: string;
}
Service.TS
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Application } from '../intdets';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IntdetsService {
private _url = "/assets/IntDet.json";
constructor(private http: HttpClient) { }
getIntdets(): Observable<Application[]> {
return this.http.get<Application[]>(this._url);
}
}
Component.TS
import { Component, OnInit } from '@angular/core';
import { IntdetsService } from '../service/intdets.service';
@Component({
selector: 'app-intdets',
templateUrl: './intdets.component.html',
styleUrls: ['./intdets.component.css']
})
export class IntdetsComponent implements OnInit {
public apps = [];
constructor(private _intdetsService: IntdetsService) { }
ngOnInit() {
this._intdetsService.getIntdets()
.subscribe(data => this.apps = data);
}
}
HTML
<ul *ngFor = "let app of apps">
<li>{{app.APP_ID}}</li> //Test code to only print one element
</ul>
据我所知,从服务返回的数据不是数组,它是一个包含 属性 数组的对象。所以 this.apps = { APPLICATIONS: [] }
如果你想迭代数组,你需要像这样访问 APPLICATIONS:
<ul *ngFor = "let app of apps.APPLICATIONS">
<li>{{app.APP_ID}}</li> //Test code to only print one element
</ul>