Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
Load elements in array dynamically in Angular

I have created a custom slider where i'm trying to load element one by one as user clicks on next and previous buttons.

Links: https://stackblitz.com/edit/angular-ivy-rwuxjd?file=src%2Fapp%2Fapp.component.ts

Initially i'm loading only first value of an array in question array

  ngOnInit(): void {
    var formData=new FormData();
    formData.append("test",'test');
    this.http.post(this.baseUrl+'api/auth/fetch', formData).subscribe(result  => {
    this.questions_lists = result;
    this.temp_questions_lists.push(this.questions_lists[0]);

questions_lists contains all the result from database but temp_questions_lists contains only first element of an array, Now on next and previous buttons i'm trying to clear array and load next value from question_lists array into temp_questions_lists array.

onNext() {
    if (this.counter != this.questions_lists.length - 1) {
      this.counter++;
      this.temp_questions_lists=[];
      this.temp_questions_lists=this.questions_lists[this.counter];
    }
  }



 onPrevious() {
    if (this.counter > 0) {
      this.counter--;
      this.temp_questions_lists=[];
      this.temp_questions_lists.push(this.questions_lists[this.counter]);
    }
  }

First time data is loaded in the slider but next time when i click on next button then i get this error :

Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed

Any solution is highly appreciated Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to use .push() instead of assigning.

onNext() {
  if (this.counter != this.questions_lists.length - 1) {
  this.counter++;
  this.temp_questions_lists=[];
  this.temp_questions_lists.push(this.questions_lists[this.counter]);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!