0

I've created a service to display a list iterating through an array. I want that list to be infinite (on scrolling). Hence my approach was to push the displayed array with itself on scroll.

This worked, but since I've decided to go with services, I can't make it function again.

Service:

import { Case } from './case';

export const CASES: Case[] = [
  { name: 'Diesel', image: '/assets/images/diesel.png', image2: '', image3: '', link: '' },
  { name: 'WeWork Berlin', image: '/assets/images/berlin.png', image2: '', image3: '', link: '' },
  { name: 'Fritzhansen', image: '/assets/images/fritzhansen.png', image2: '', image3: '', link: '' },
  { name: 'Savum', image: '/assets/images/savum.png', image2: '/assets/images/savum/savum-logo.png',
    image3: '/assets/images/savum/savum-iphone.png', link: '' },
  { name: 'Eskay', image: '/assets/images/eskay.png', image2: '', image3: '', link: '' },
  { name: 'Diesel', image: '/assets/images/diesel-snd.png', image2: '', image3: '', link: '' },
  { name: 'Mobilia', image: '/assets/images/mobilia.png', image2: '', image3: '', link: '' },
  { name: 'Rarekind', image: '/assets/images/rarekind.png', image2: '', image3: '', link: '' }
];


To iterate through this array, I fetched it inside of the case-list component.ts:

import {Component, OnInit} from '@angular/core';
import { CASES } from '../mock/mock-cases';

@Component({
  selector: 'app-case-list',
  templateUrl: './case-list.component.html',
  styleUrls: ['./case-list.component.scss']
})
export class CaseListComponent implements OnInit {

  cases = CASES;

  ...


How can I push cases again, so that it contains multiple CASES arrays?

I've tried: this.cases.push(CASES), but this one won't work.

I want cases to look like this (CASES x3):

{ name: 'Diesel', image: '/assets/images/diesel.png', image2: '', image3: '', link: '' },
  { name: 'WeWork Berlin', image: '/assets/images/berlin.png', image2: '', image3: '', link: '' },
  { name: 'Fritzhansen', image: '/assets/images/fritzhansen.png', image2: '', image3: '', link: '' },
  { name: 'Savum', image: '/assets/images/savum.png', image2: '/assets/images/savum/savum-logo.png',
    image3: '/assets/images/savum/savum-iphone.png', link: '' },
  { name: 'Eskay', image: '/assets/images/eskay.png', image2: '', image3: '', link: '' },
  { name: 'Diesel', image: '/assets/images/diesel-snd.png', image2: '', image3: '', link: '' },
  { name: 'Mobilia', image: '/assets/images/mobilia.png', image2: '', image3: '', link: '' },
  { name: 'Rarekind', image: '/assets/images/rarekind.png', image2: '', image3: '', link: '' },

{ name: 'Diesel', image: '/assets/images/diesel.png', image2: '', image3: '', link: '' },
  { name: 'WeWork Berlin', image: '/assets/images/berlin.png', image2: '', image3: '', link: '' },
  { name: 'Fritzhansen', image: '/assets/images/fritzhansen.png', image2: '', image3: '', link: '' },
  { name: 'Savum', image: '/assets/images/savum.png', image2: '/assets/images/savum/savum-logo.png',
    image3: '/assets/images/savum/savum-iphone.png', link: '' },
  { name: 'Eskay', image: '/assets/images/eskay.png', image2: '', image3: '', link: '' },
  { name: 'Diesel', image: '/assets/images/diesel-snd.png', image2: '', image3: '', link: '' },
  { name: 'Mobilia', image: '/assets/images/mobilia.png', image2: '', image3: '', link: '' },
  { name: 'Rarekind', image: '/assets/images/rarekind.png', image2: '', image3: '', link: '' },

{ name: 'Diesel', image: '/assets/images/diesel.png', image2: '', image3: '', link: '' },
  { name: 'WeWork Berlin', image: '/assets/images/berlin.png', image2: '', image3: '', link: '' },
  { name: 'Fritzhansen', image: '/assets/images/fritzhansen.png', image2: '', image3: '', link: '' },
  { name: 'Savum', image: '/assets/images/savum.png', image2: '/assets/images/savum/savum-logo.png',
    image3: '/assets/images/savum/savum-iphone.png', link: '' },
  { name: 'Eskay', image: '/assets/images/eskay.png', image2: '', image3: '', link: '' },
  { name: 'Diesel', image: '/assets/images/diesel-snd.png', image2: '', image3: '', link: '' },
  { name: 'Mobilia', image: '/assets/images/mobilia.png', image2: '', image3: '', link: '' },
  { name: 'Rarekind', image: '/assets/images/rarekind.png', image2: '', image3: '', link: '' }
3
  • examples are very hard to read. consider making a simplified version of the examples Commented Jun 17, 2019 at 21:33
  • 1
    @gatsbyz it's just the standard CASES service, multiplied by 3 - as mentioned Commented Jun 17, 2019 at 21:34
  • 1
    Have you tried this? this.cases.push(...CASES) Commented Jun 17, 2019 at 21:39

1 Answer 1

1

You should try this.cases.push(...CASES). It's called the spread syntax.

For function calls:

myFunction(...iterableObj);
For

array literals or strings:

[...ite`rableObj, '4', 'five', 6];

For object literals (new in ECMAScript 2018):

let objClone = { ...obj };
Sign up to request clarification or add additional context in comments.

9 Comments

This one seems to work, but it instantly adds thousands of entries on scroll to the array which causes the browser to freeze.
@Tim Where do you add on to the array? ngOnInit?
To create infinite scrolling with the repeating list I push the array on (scroll) - I think that might be the problem, any bypass? I did this before, but by solely pushing a usual array - didn't cause any issues.
Is there a way to add CASES in a more "controlled" way?
@Tim Can you add if (cases.length < CASES.length * 3) { to that?
|

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.