# @ViewChild(Content) - content is undefined and I can't scroll to bottom

**URL:** <https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981>\
**Category:** ionic-v3\
**Created:** [October 24, 2016, 12:46am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981 "2016-10-24T00:46:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Corin](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@Corin](https://forum.ionicframework.com/u/Corin)\
**Post date:** [October 24, 2016, 12:46am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/1 "2016-10-24T00:46:40Z")

</div>

Hi,

I’ve just detected that sometimes when I add rows to my list and try to scroll to bottom using:

```
//@ViewChild(Content) content: Content;
...
this.content.scrollToBottom();

```

my content var is undefined sometimes, so I can’t scroll down.

Any help, please?

Thank you.

---

<div class="post-metadata">

**Author:** ![lucasbasquerotto](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lucasbasquerotto/32/101719_2.png) [@lucasbasquerotto](https://forum.ionicframework.com/u/lucasbasquerotto)\
**Post date:** [October 24, 2016, 10:57am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/2 "2016-10-24T10:57:03Z")

</div>

@Corin I don’t know when you are calling the content method in your code, but did you put that call inside `ngOnInit`, `ngOnChanges` or something like that? **Maybe the view was not initiated yet.** Try to use only after the view was loaded (after `ngAfterViewInit` was called).

---

<div class="post-metadata">

**Author:** ![Corin](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@Corin](https://forum.ionicframework.com/u/Corin)\
**Post date:** [October 24, 2016, 10:23pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/3 "2016-10-24T22:23:39Z")

</div>

Thank you for your help Lucas, but no I am calling it just when I enter some message in my chat. :S

Maybe I need to give enough time to the list grow?

---

<div class="post-metadata">

**Author:** ![lucasbasquerotto](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/lucasbasquerotto/32/101719_2.png) [@lucasbasquerotto](https://forum.ionicframework.com/u/lucasbasquerotto)\
**Post date:** [October 25, 2016, 10:28am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/4 "2016-10-25T10:28:28Z")

</div>

@Corin Content should be defined. **Are you using `@ViewChild(Content)` in the same component that has the `ion-content` tag in the html template?** (that is, it’s not a super-component nor a sub-component)

---

<div class="post-metadata">

**Author:** ![Corin](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@Corin](https://forum.ionicframework.com/u/Corin)\
**Post date:** [October 25, 2016, 9:17pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/5 "2016-10-25T21:17:51Z")

</div>

Yes, in the same component and the problem is android only. ☹

---

<div class="post-metadata">

**Author:** ![Masumi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/masumi/32/127715_2.png) [@Masumi](https://forum.ionicframework.com/u/Masumi)\
**Post date:** [August 13, 2019, 11:56am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/6 "2019-08-13T11:56:30Z")

</div>

I also had the same problem, but it worked out after `ionViewDidEnter` .

```auto
import {ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';

-----//-----

export class SamplePage {
  @ViewChild(Content) content: Content;

  constructor( ) {
    console.log(this.content); // => undefined
  }

  ionViewDidEnter() {
    console.log(this.content); // => Content {_config: Config, …}
    this.content.scrollToBottom(); // => worked correctly
  }
}

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 13, 2019, 5:32pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/7 "2019-08-13T17:32:49Z")

</div>

As I understand it, this timing issue is related to the `static` property of `@ViewChild`, which didn’t exist when this thread was created, but is important now and will be mandatory going forward.

---

<div class="post-metadata">

**Author:** ![flavio\_b](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@flavio\_b](https://forum.ionicframework.com/u/flavio_b)\
**Post date:** [January 27, 2022, 5:49pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/8 "2022-01-27T17:49:57Z")

</div>

Hi!

I am experiencing the same in Ionic 6, but I’m wondering if the only solution to implement something like scrollToTop() is to create one dummy function ionViewDidEnter() which is scrolling to top (just to initialize such content variable) or if we have one more solution.

---

<div class="post-metadata">

**Author:** ![Masumi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/masumi/32/127715_2.png) [@Masumi](https://forum.ionicframework.com/u/Masumi)\
**Post date:** [January 28, 2022, 1:34am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/9 "2022-01-28T01:34:14Z")

</div>

@flavio_b  
Yes, it is the best way to implement that function to solve the solution.  
Although it is not the best practice (in my opinion), you may also write asynchronous functions in the background and trigger it before landing on the page.

I hope it helps you 😄

---

<div class="post-metadata">

**Author:** ![flavio\_b](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@flavio\_b](https://forum.ionicframework.com/u/flavio_b)\
**Post date:** [January 28, 2022, 8:00am UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/10 "2022-01-28T08:00:30Z")

</div>

Hi! Thank you for replying.  
Unfortunately this didn’t solve for me totally.  
Such function ionViewDidEnter() makes the content variable no longer undefined now:

```auto
import { Component, ViewChild } from '@angular/core';
import {
  NavController,
  NavParams,
  LoadingController,
  IonContent,
  AlertController,
  ScrollBaseDetail,
} from '@ionic/angular';

@Component({
  selector: 'app-page-lista-atleti',
  templateUrl: 'lista-atleti.html',
})
export class ListaAtletiPage {
  constructor() { }

  ionViewDidEnter() {
    console.log(this.content); // => Content {_config: Config, …}
    this.content.scrollToTop(); // => worked correctly
  }

  @ViewChild('myComponent', { static: false }) content: IonContent;

}

```

And it also prints out: “core.mjs:6495 ERROR TypeError: this.content.scrollToTop is not a function”

---

<div class="post-metadata">

**Author:** ![Masumi](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/masumi/32/127715_2.png) [@Masumi](https://forum.ionicframework.com/u/Masumi)\
**Post date:** [January 28, 2022, 3:57pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/11 "2022-01-28T15:57:41Z")

</div>

@flavio_b  
It might solve the problem by updating the version (check [this discussion](https://forum.ionicframework.com/t/ionviewdidenter-not-working/209158/6)).

Also, the way I recommended for ionic3 might be deprecated in ionic 6. Therefore, you should use the coding rules in the new document. I will share the link which might help you.

> **[ion-content | Ionic Documentation](https://ionicframework.com/docs/api/content)**
>
> ion-content: Scrollable CSS Component for Ionic App Content Areas

---

<div class="post-metadata">

**Author:** ![flavio\_b](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@flavio\_b](https://forum.ionicframework.com/u/flavio_b)\
**Post date:** [January 28, 2022, 5:27pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/12 "2022-01-28T17:27:23Z")

</div>

Thanks!!  
I’ve been in that documentation page for at least 3/4 days so far.  
For what I see, it suggests to add the following to the ion-content and nothing more:

```auto
<ion-content
  [scrollEvents]="true"
  (ionScrollStart)="logScrollStart()"
  (ionScroll)="logScrolling($event)"
  (ionScrollEnd)="logScrollEnd()">

</ion-content>

```

And this is working to me, if I add all those three methods to my .ts file.  
But the documentation doesn’t say anything about handling the button event and triggering the `scrollToTop()` function that should be something like this:

```auto
scrollToTop() {
    this.content.scrollToTop(1500);
  }

  ionViewDidEnter() {
    console.log(this.content); // => Content {_config: Config, …}
    this.content.scrollToTop(); // => worked correctly
  }

  @ViewChildren(IonContent) content: IonContent;

```

The thing is that such scrollToTop() function is not a function for the framework! Why? Now that the this.content is not undefined, I should be able to call that function.

Also, for what I see [here](https://ionicframework.com/docs/api/content?_gl=1*6gy7wr*_ga*OTEzMzQyMTM3LjE2MzE0NzI1Mjg.*_ga_REH9TJF6KF*MTY0MzM4ODIzNi42Ni4xLjE2NDMzODk2NjguMA..#scrolltotop), I should be able to call `scrollToTop()` but it is not true.

I’m on Ionic/Angular 6.0.2.

---

<div class="post-metadata">

**Author:** ![flavio\_b](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@flavio\_b](https://forum.ionicframework.com/u/flavio_b)\
**Post date:** [February 2, 2022, 8:12pm UTC](https://forum.ionicframework.com/t/viewchild-content-content-is-undefined-and-i-cant-scroll-to-bottom/67981/13 "2022-02-02T20:12:29Z")

</div>

Hello!

I think I’ve found out why it was not working so far.  
I have solved adding FormsModule and IonicModule to the imports section of the module.ts page module class:

```auto
@NgModule({
  declarations: [MyPage],
  imports: [
    MyPageRoutingModule,
    PipesModule,
    DirectivesModule,
    CommonModule,
    FormsModule,
    IonicModule,
  ],
})

```

Hope this helps someone else having the same problem.
