0

FYI I am very new to Angular2 - basically I'm calling a data service that returns a promise of an array of objects, which I bind in my view and loop through them. It is working great, but I started exploring how to use a infinite scroll which would require me to append onto the array, but I cannot get this to work.

This is how I declare the array in the component (typescript):

integrationCollection: IntegrationEntity[] = [];

And this is the data service call:

this.integrationService.getIntegrations().then(integrationCollection => this.integrationCollection = integrationCollection);

And the results are displayed just fine on the page. But the length property returns 0.. why would this be ??

alert('integrationCollection count: ' + this.integrationCollection.length); // returns 0

Also, I'm going to need to be able to add to my array for an infinite scroll scenario, so I think I need to be able to concat the array - but this is not working at all (no errors, but nothing is displayed on the page):

this.integrationService.getIntegrations().then(integrationCollection => this.integrationCollection.concat(integrationCollection));

I think maybe these two problems are related? I think I'm just missing something fundamental.

Thanks for any insight.

2
  • 2
    For the first thing, if I had to guess, it's because the alert is triggering before the getIntegrations() call is finished. I'm not sure about the second issue. Commented Oct 4, 2016 at 20:44
  • Thanks, got it. Second issue is because I need to do an assignment: this.integrationService.getIntegrations().then(integrationCollection => this.integrationCollection = this.integrationCollection.concat(integrationCollection)); Commented Oct 4, 2016 at 21:10

2 Answers 2

1

This is because of async behavior of framework. Change following of your code:

this.integrationService.getIntegrations().then(integrationCollection => this.integrationCollection = integrationCollection);
alert('integrationCollection count: ' + this.integrationCollection.length);

to:

this.integrationService.getIntegrations().then(integrationCollection => {
this.integrationCollection = integrationCollection;
alert('integrationCollection count: ' + this.integrationCollection.length);
 });

Do you see the difference? There are other ways as well to bring synchronus behavior in your code when you need i.e. Observable.forkJoin. Hope you got the idea.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, very helpful.
0

In your code :

integrationCollection => this.integrationCollection.concat(integrationCollection)

You probably want to assign the result of the concat somewhere e.g. :

integrationCollection => this.integrationCollection = this.integrationCollection.concat(integrationCollection)

More

But the length property returns 0.. why would this be ??

Most likely an ordering issue. You should check then length after the data has actually returned from the server.

Comments

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.