html
fragment1
component.ts
<div class="py-4" *ngFor="let data of Question; let i = index">
{{data | json}}
</div>
service.ts
ngOnInit(){
this.showQuestion();
}
Question: any [];
showQuestion() {
const Entity = new TaskViewEntity(someParameter, 'QuestionDetails');
this.serviceParameter.TaskApi(Entity.requestFormat).subscribe(res => {
if (res['result'].questionDetails !== undefined) {
const arr = res['result'].questionDetails.questions;
this.Question = arr.map(item => {
return {
'counter': 1,
'answer': item.answer,
'approveFlag': item.approveFlag,
'options': item.options,
'question': item.question,
'questionId': item.questionId,
'questionType': item.questionType,
'subQuestion': item.subQuestion
};
});
}
});}
Json
public TaskApi = (data) => {
const url = 'URL';
return this.http.post(url, data);
}
使用console.log(this.Question)后,我得到此结果
"questions": [
{
"questionId": 44,
"question": "abc",
"questionType": "checkbox",
},
{
"questionId": 44,
"question": "abc",
"questionType": "checkbox",
},
{
"questionId": 44,
"question": "abc",
"questionType": "checkbox",
},
]
上面提到的代码不起作用,它在我的控制台中显示了错误:无法找到类型为'object'的其他支持对象'[object Object]'。 NgFor仅支持绑定到Iterables,例如数组。有角度的。我无法访问问题。
答案 0 :(得分:0)
我认为您首先没有初始化数组问题,而是在subscribe中对其进行初始化,因此在此之前尚未初始化,您可以尝试这样做:
Question: any[] = [];