1

I'm trying to display data fetched from laravel api in angular table code is :

public function fetchQuestions(Request $request)
{  
  $questions = Questions::where('status',$request->status)->get();
  return response()->json($questions, 200);
}

angular code:

 questions = [];
  ...
  this.http.post(this.baseUrl+'api/auth/question', formData).subscribe(result  => {
  this.questions.push(result);

This does not display any data

i have to use

  this.questions.push(result[0]);
  this.questions.push(result[1]);
  ..
  ..
  this.questions.push(result[n]);

to push all the data. How can i push all array data.

and display data using loop

<ng-container *ngFor="let img of questions; let i=index">

1 Answer 1

1

You are currently pushing an array into an array. To get the result you want you should just assign the result array to the questions array:

...subscribe(result => {this.questions = result})
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, Now getting this error The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
How are the questions defined? Are they a specific data type? If you do change this.http.post() to this.http.post<DATATYPE[]>(), if not you can use this.http.post<any[]>()
I have defined it as array questions = [];
See my edit. You could also define questions as questions:any = []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.