Skip to content

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

Closed

Description

@JumBay

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

Activity

  1. paulstelzer commented on Dec 7, 2018

    @paulstelzer
    Contributor

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

  2. added
    needs: reply
    and removed on Dec 7, 2018
  3. JumBay commented on Dec 7, 2018

    @JumBay
    Author

    Same issue on beta.17

  4. added and removed
    needs: reply
    on Dec 7, 2018
  5. added a commit that references this issue on Dec 13, 2018
    392c219
  6. added a commit that references this issue on Dec 13, 2018
    f05c7d6
  7. JumBay commented on Dec 17, 2018

    @JumBay
    Author

    I still facing this issue on beta.19

  8. manucorporat commented on Dec 17, 2018

    @manucorporat
    Contributor

    @JumBay the fix was merged but not released :)

  9. 1 remaining item

  10. manucorporat commented on Dec 17, 2018

    @manucorporat
    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.

  11. added
    needs: reply
    on Dec 17, 2018
  12. paulstelzer commented on Dec 17, 2018

    @paulstelzer
    Contributor

    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

  13. added and removed
    needs: reply
    on Dec 17, 2018
  14. added
    needs: reply
    and removed
    package: core
    on Dec 17, 2018
  15. JumBay commented on Dec 17, 2018

    @JumBay
    Author

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

  16. added and removed
    needs: reply
    on Dec 17, 2018
  17. ionitron-bot commented on Jan 16, 2019

    @ionitron-bot

    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.

  18. locked and limited conversation to collaborators on Jan 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions