Не удается найти другой поддерживающий объект '[object Object]' типа 'object'. NgFor поддерживает только привязку к итерациям, таким как массивы. угловатый - PullRequest
0 голосов
/ 25 августа 2018

html

 <div class="py-4" *ngFor="let data of Question; let i = index">
   {{data | json}}
  </div>

component.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
      };
    });
  }
});}

service.ts

public TaskApi = (data) => {
   const url = 'URL';
   return this.http.post(url, data);
}

Json

 "questions": [
      {
         "questionId": 44,
         "question": "abc",
         "questionType": "checkbox",
      },
      {
         "questionId": 44,
         "question": "abc",
         "questionType": "checkbox",
       },
       {
          "questionId": 44,
          "question": "abc",
          "questionType": "checkbox",
       },
   ]

После использования консоли.log (this.Question), я получаю этот результат

(3) [{…}, {…}, {…}]
0: {counter: 1, answer: "", approveFlag: true, options: {…}, question: "Please confirm the address of the property is", …}
1: {counter: 1, answer: "", approveFlag: true, options: {…}, question: "Please confirm the name of the seller?", …}
2: {counter: 1, answer: "", approveFlag: true, options: {…}, question: "Please confirm the number of sellers/partners?", …}

Вышеупомянутый код не работает, это показывает ошибку в моей консоли, которая: Не удается найти другой поддерживающий объект '[object Object]' извведите «объект».NgFor поддерживает только привязку к итерациям, таким как массивы.Угловой.Я не могу получить доступ к вопросам.

1 Ответ

0 голосов
/ 25 августа 2018

Я думаю, что вы не инициализировали вопросы массива в первую очередь, вы инициализируете его в подписке, поэтому до тех пор, пока он не будет инициализирован, вы можете попробовать выполнить: Question: any[] = [];

...