【发布时间】:2017-06-06 15:15:47
【问题描述】:
我正在尝试从本地 json 文件中读取一些测试数据,并将格式正确的数据输出到 textarea 中。现在虽然它只是输出 [object Object]。我将如何获得它以便输出:
Id: theIdGoesHere
标题:theTitleGoesHere
step.service.ts 用来调用json数据的服务
public getJson(): Observable<any>{
return this.http.get('/assets/jsonData/MyJson.json')
.map(response => response.json());
}
MyJson.json
{
"data":[
{
"id": 1,
"title":"Test1"
},
{
"id": 2,
"title":"Test2"
}
]
}
main.componenet.ts
private testVar: any;
test(){
this.stepService.getJson().subscribe(data => (this.testVar = data));
}
anothermethod(){
this.test();
this.mainStepText = this.testVar; //mainStepText binded to textarea with [(ngModel)]="mainStepText"
}
get mainStepText2() { //Rebinded this one
const text = [];
const { data } = this.testVar;
for (let item of this.testVar.data) {
Object.keys(item).forEach(key => {
text.push(key + ': ' + item[key]);
});
}
return text.join('\r\n'); // \r\n is the line break
}
【问题讨论】:
标签: json angular typescript