Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4.0.0-beta.13 - VirtualScroll doesn't call ngOnInit of all its children #16023

Closed
JumBay opened this issue Oct 21, 2018 · 8 comments · Fixed by #16729
Closed

V4.0.0-beta.13 - VirtualScroll doesn't call ngOnInit of all its children #16023

JumBay opened this issue Oct 21, 2018 · 8 comments · Fixed by #16729
Labels

Comments

@JumBay
Copy link

JumBay commented Oct 21, 2018

Bug Report

Ionic Info
Run ionic info from a terminal/cmd prompt and paste the output below.

Ionic:

   ionic (Ionic CLI)             : 4.2.1 (/usr/local/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.0.0-beta.13
   @angular-devkit/build-angular : 0.8.6
   @angular-devkit/schematics    : 0.8.6
   @angular/cli                  : 6.2.6
   @ionic/angular-toolkit        : 1.0.0

System:

   NodeJS : v8.12.0 (/usr/local/bin/node)
   npm    : 6.4.1
   OS     : macOS

Describe the Bug
Just run this simple code on a fresh project:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
  <ion-app>
      <ion-content>
        <ion-list>
            <ion-virtual-scroll [items]="movies" approxItemHeight="44px">
              <app-movie *virtualItem="let item" [title]="item"></app-movie>
            </ion-virtual-scroll>
        </ion-list>
      </ion-content>
  </ion-app>
`
})
export class AppComponent implements OnInit {
  movies: string[] = [];

  ngOnInit() {
    for (let i = 0; i < 1000; i++) {
      this.movies.push(`The Movie ${i}`);
    }
  }
}

@Component({
  selector: 'app-movie',
  template: `
  <ion-item>{{title}}</ion-item>
`
})
export class MovieComponent implements OnInit {
  @Input()
  title: string;

  ngOnInit() {
    console.log('On Init', this.title);
  }
}

If you check your console, you'll notice the ngOnInit() method is not called for all MovieComponent, only some of them

@ionitron-bot ionitron-bot bot added the triage label Oct 21, 2018
@paulstelzer
Copy link
Contributor

Thanks for your issue! Could you please check if this is still an issue in beta.17? That would be very nice :)

@paulstelzer paulstelzer added needs: reply the issue needs a response from the user and removed triage labels Dec 7, 2018
@JumBay
Copy link
Author

JumBay commented Dec 7, 2018

Same issue on beta.17

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Dec 7, 2018
@paulstelzer paulstelzer added needs: investigation package: core @ionic/core package and removed triage labels Dec 7, 2018
manucorporat added a commit that referenced this issue Dec 13, 2018
fixes #16725
fixes #16432
fixes #16023
fixes #14591
fixes #16050
fixes #15587
pull bot pushed a commit to stackriot/ionic that referenced this issue Dec 13, 2018
@JumBay
Copy link
Author

JumBay commented Dec 17, 2018

I still facing this issue on beta.19

@manucorporat
Copy link
Contributor

@JumBay the fix was merged but not released :)

@manucorporat manucorporat reopened this Dec 17, 2018
@manucorporat
Copy link
Contributor

@JumBay I misunderstood the problem, I think this is the expected behaviour. Virtual-scroll creates a set of components then it reuses, is it not that the point of a virtual-scroll?

Your component should be stateless if you want to use it with virtual-scroll, only depending in the inputs the component has.

@manucorporat manucorporat added the needs: reply the issue needs a response from the user label Dec 17, 2018
@paulstelzer
Copy link
Contributor

paulstelzer commented Dec 17, 2018

As @manucorporat says - the components will be reused. So ngOnInit will not be called (the component already exist). Virtual scroll only create components who are necessary to fill the viewport.

So you have to look for the changes of this component

ngOnChanges(changes) {
   console.log('CHANGES', changes.title.currentValue);
}

And then you will see it :) I also need some time to understand it. So keep in mind: Components are REUSED!

chrome_2018-12-17_14-28-11

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Dec 17, 2018
@paulstelzer paulstelzer added needs: reply the issue needs a response from the user and removed needs: investigation package: core @ionic/core package triage labels Dec 17, 2018
@JumBay
Copy link
Author

JumBay commented Dec 17, 2018

Thanks that's what I thought and I updated my code in this direction.

@JumBay JumBay closed this as completed Dec 17, 2018
@ionitron-bot ionitron-bot bot removed the needs: reply the issue needs a response from the user label Dec 17, 2018
@ionitron-bot ionitron-bot bot added the triage label Dec 17, 2018
@ionitron-bot
Copy link

ionitron-bot bot commented Jan 16, 2019

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Jan 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants