gpt4 book ai didi

json - 如何以 Angular 4打印 Angular 对象

转载 作者:行者123 更新时间:2023-12-04 12:50:09 31 4
gpt4 key购买 nike

如何打印 json 值,我很像 angular,请有人帮帮我。

test.html

{{ projects }}
<li *ngFor="let obj of projects">{{obj | json }}</li>

services.ts
getProjects(): Promise<Addtimesheet[]> {
return this.http.get("http://192.168.1.90/EMS/api/TimeSheetAPI/getProjects")
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
console.log("Sdfsdfsdfsd sss");
console.log(body);
return body;
}

private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}

组件.ts
import { Component, OnInit } from '@angular/core';
import { Addtimesheet } from './addtimesheet';

import { TimesheetService } from './timesheet.service';

import { Projects } from './projects';

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

timesheet = {};
today: any;

private projects: Projects[] = [];
private errorMessage: any = '';

constructor(private timesheetService: TimesheetService) {
}

ngOnInit() {
this.timesheetService.getProjects()
.then(projects => this.projects = projects)
console.log(this.timesheetService.getProjects());
}
}

json值
[
"AESEMS",
"ChainDrive",
"CICAND",
"CICAPI",
"CICiOS",
"CICWeb",
"KC_APPTV",
"KCMagento",
"RDLSWeb",
"Riddles",
"TB",
"TBAND",
"TBiOS",
"TestProject"
]

我试过了,错误错误:找不到“对象”类型的不同支持对象“[对象对象]”。 NgFor 仅支持绑定(bind)到 Iterables,例如 Arrays。

显示某种错误。

最佳答案

要打印 JSON:

<pre> 
<code>
{{ obj | json }}
</code>
</pre>

或者将其打印到控制台:
console.log(JSON.stringify(obj));

关于json - 如何以 Angular 4打印 Angular 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021514/

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