# Open page already scrolled at bottom (chat app)

**URL:** <https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634>\
**Category:** ionic-v3\
**Created:** [January 27, 2017, 5:50pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634 "2017-01-27T17:50:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [January 27, 2017, 5:50pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/1 "2017-01-27T17:50:56Z")

</div>

Hi, I am developing a chat app, and I would like to be able to open the chat component already scrolled at the bottom.

I am currently using to `this.content.scrollToBottom(0)` on the `ionViewDidEnter()` function, but that renders the page scrolled at the top first, and then scrolls down. I have already tried to put the scrollToBottom at the `ionViewWillEnter()` but that doesn’t work, I guess it’s because the page is not rendered yet.

The chat page is being opened with `this.navCtrl.push(ConversaPage, {some:'parms'})` when the user clicks on the chat list item.

The messages are a `<div *ngOf="let msg of msgs">`.

I have already tried to user the angular2-auto-scroll directive, putting it at the ion content, but it didn’t scroll the content…

Could you help me with that? The gif below shows how it is working now, it first opens the page on the top, then scrolls down. It would be great if there was a way to already open at the bottom.

![image](https://global.discourse-cdn.com/ionicframework/original/3X/a/a/aa711dd38205d1a2429c2dfbcae9c41774f3c6c1.gif)

Can’t update everything because a lot of styles have changed, just sending the version info in case it could help with anything.

```
Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.36
ios-deploy version: 1.9.0 
ios-sim version: 5.0.8 
OS: macOS Sierra
Node Version: v4.6.0
Xcode version: Xcode 7.3.1 Build version 7D1014
```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [January 27, 2017, 6:04pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/2 "2017-01-27T18:04:38Z")

</div>

You should be able to do

```auto
@Component({
  selector: 'page-page2',
  templateUrl: 'page2.html'
})
export class Page2 {
  @ViewChild(Content) content: Content
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams) { }
  ionViewWillEnter() {
    this.content.scrollToBottom(0)
  }
}

```

And scroll when the page is about to enter

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [January 27, 2017, 7:02pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/3 "2017-01-27T19:02:04Z")

</div>

Thanks for the quick response @mhartington!

I have tried using ionViewWillEnter as stated in the original post, but it just doesn’t scroll to the bottom, it gets executed when the page will open but it opens at the top. Isn’t it because the page isn’t rendered yet? What causes the page to be big enough to scroll are the messages that are bound to the `<div *ngOf="let msg of msgs">`, are these rendered before the ionViewWillEnter?

Thanks again

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [January 27, 2017, 7:59pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/4 "2017-01-27T19:59:52Z")

</div>

Try this.

```auto
<ion-content>
  <ion-list>
    <ion-item *ngFor="let item of items; let last = last">
      {{item}}
      {{last ? callFunction() : ''}}
    </ion-item>
  </ion-list>
</ion-content>

```

```auto
export class Page2 {
  public items: any[] = [];
  @ViewChild(Content) content: Content
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams) {
    setTimeout(() => {
      for (let i = 0; i < 100; i++) {
        this.items[i] = i
      }
    }, 300)
  }
  callFunction(){
    this.content.scrollToBottom(0)
  }
}

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [January 27, 2017, 8:01pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/5 "2017-01-27T20:01:04Z")

</div>

So a bit about whats going on here…

Basically you bind to the `last` variable for ngFor, so when that gets rendered, it will call the method in our class, so no matter how much data we need to render on pageEnter, it will always load at the bottom.

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [January 27, 2017, 8:04pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/6 "2017-01-27T20:04:48Z")

</div>

Got it! That’s something I didn’t try, will get back to you with the result!

Thanks!

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [January 27, 2017, 8:23pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/7 "2017-01-27T20:23:40Z")

</div>

That did it @mhartington! Thanks a lot for your quick and effective help!  
It’s a great solution because now I don’t have to subscribe to an event emitter on every new message, with this it will scroll down when a new message is added automatically.

Thanks!

---

<div class="post-metadata">

**Author:** ![pcmquizon](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@pcmquizon](https://forum.ionicframework.com/u/pcmquizon)\
**Post date:** [March 6, 2017, 1:22pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/8 "2017-03-06T13:22:46Z")

</div>

Thanks this was what I was looking for. \<3

---

<div class="post-metadata">

**Author:** ![lilbiscuit](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lilbiscuit](https://forum.ionicframework.com/u/lilbiscuit)\
**Post date:** [May 9, 2017, 12:41am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/9 "2017-05-09T00:41:34Z")

</div>

Is this possible with Ionic V1 ?

---

<div class="post-metadata">

**Author:** ![firebaseInfoObjects](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@firebaseInfoObjects](https://forum.ionicframework.com/u/firebaseInfoObjects)\
**Post date:** [June 13, 2017, 2:06pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/10 "2017-06-13T14:06:03Z")

</div>

This works great. But this function keeps getting called in my case for some reason so even if I’m trying to scroll upwards, it scrolls back to the bottom. Please help.

---

<div class="post-metadata">

**Author:** ![ravi\_sojitra](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@ravi\_sojitra](https://forum.ionicframework.com/u/ravi_sojitra)\
**Post date:** [July 31, 2017, 9:43am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/12 "2017-07-31T09:43:59Z")

</div>

Did you find any solution because i am having similar issue.  
It keeps scrolling back to the bottom.

---

<div class="post-metadata">

**Author:** ![ravi\_sojitra](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@ravi\_sojitra](https://forum.ionicframework.com/u/ravi_sojitra)\
**Post date:** [July 31, 2017, 10:17am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/13 "2017-07-31T10:17:59Z")

</div>

This solution is working fine. But it keeps getting called for some reason. When i am trying to scroll upwards, it keeps getting scrolled back to the bottom.

Can you tell me why is that happening?

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [July 31, 2017, 4:44pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/14 "2017-07-31T16:44:37Z")

</div>

@ravi_sojitra @firebaseInfoObjects  
Just guessing, assuming you are using \*ngFor with the “last” attribution, one possible reason would be that the variable that is being iterated over is being changed multiple times. e.g.:

```auto
<div *ngFor="let var of vars; let last = last">
    {{var}}
    {{last ? scrollDown() : ''}}
</div>

```

If `vars` is changing all the time, the elements will be iterated over again, calling the `scrollDown()` function again.  
Make sure you are iterating over an array, not on a function’s response, otherwise on every context update ionic runs the function again, iterating over the elements again, calling the `scrollDown()` function again.

Hope this helps

---

<div class="post-metadata">

**Author:** ![ravi\_sojitra](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@ravi\_sojitra](https://forum.ionicframework.com/u/ravi_sojitra)\
**Post date:** [August 1, 2017, 4:20am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/15 "2017-08-01T04:20:44Z")

</div>

Hi there,  
Thanks for response.

I am getting datas from php web service and then pushing results into an array. And i am iterating over that array to display the result and then there is this scrollDown function, Nothing else.

Will that cause any problem?

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [August 1, 2017, 4:57pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/16 "2017-08-01T16:57:54Z")

</div>

@ravi_sojitra could you paste your \*ngFor code here? If possible the code that retrieves the php webservice too…  
If you are calling a function in the \*ngFor statement, it will be executed several times.

---

<div class="post-metadata">

**Author:** ![ravi\_sojitra](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@ravi\_sojitra](https://forum.ionicframework.com/u/ravi_sojitra)\
**Post date:** [August 2, 2017, 4:16am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/17 "2017-08-02T04:16:55Z")

</div>

This is \*ngFor code here.

```auto
<ion-item *ngFor="let item of msgs; let last = last"> {{last ? scrollToBottom() : ''}}
      <div class="chatBubble">
        <img *ngIf="item.place == 'left'" class="profile-pic {{item.place}}" src="{{item.profile}}">
        <div class="chat-bubble {{item.place}}">
          <div *ngIf="item.msg_type == 1" class="message">{{item.msg}}</div>
          <img *ngIf="item.msg_type == 2" src="{{item.msg}}" imageViewer class="{{item.place}} msgImage" >
        </div>
      </div>
  </ion-item>

```

Sorry for before… i am using sqlite to get those msg datas.

```auto
loadChat(loggedUserID,toUserID){

this.sqlite.getChat(loggedUserID,toUserID).then((data)=>{

      // alert(JSON.stringify(data));
      // alert(JSON.stringify(data['rows']));

      var place ='';
      for(var i = 0; i < data['rows'].length; i++) {
        if(data['rows'].item(i).fromUser == this.userID){
          place = 'right';
        } else {
          place = 'left';
        }
        this.msgs.push({
          place:place, 
          profile: this.userDetail.userPic,
          msg:data['rows'].item(i).message,
          thumb:data['rows'].item(i).thumb,
          msg_type : data['rows'].item(i).msgType
        });
      }
    }).catch((err)=>{
      //alert("error while fetching data");
      alert("error fetch"+JSON.stringify(err));
    });
    // this.scrollToBottom();
  }

```

And this is my sqlite code to get datas

```auto
public getChat(loggedUserID,toUserID) {

    var SelectQuery = "SELECT * FROM chat where (toUser=? AND fromUser=?) OR (toUser=? AND fromUser=?) ";

    return new Promise((resolve,reject) => {

      window.sqlitePlugin.openDatabase(this.dbData,(db)=>{
          db.executeSql(SelectQuery,[toUserID,loggedUserID,loggedUserID,toUserID],(data)=>{
            if(data.rows.length > 0) {
                resolve(data);
            }
          },(error)=>{
            //alert("did not get chat");
            
            reject("can not get previous chat "+error);
          })
        });
    })
  }

```

---

<div class="post-metadata">

**Author:** ![paulovitorjp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/paulovitorjp/32/104257_2.png) [@paulovitorjp](https://forum.ionicframework.com/u/paulovitorjp)\
**Post date:** [August 2, 2017, 6:11pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/18 "2017-08-02T18:11:24Z")

</div>

@ravi_sojitra from your code I don’t see a reason for the scrollBottom() function be called multiple times, unless when there are new messages being pushed to the msgs array.  
You are updating the msgs array inside the loadChat() function, then I would check where you are calling it from.  
If you are calling loadChat() multiple times, or updating the msgs array from other place, that would be a possible cause.  
Or even look for scrollBottom() callers, maybe it is not being called from the last check in the \*ngFor.

---

<div class="post-metadata">

**Author:** ![ravi\_sojitra](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@ravi\_sojitra](https://forum.ionicframework.com/u/ravi_sojitra)\
**Post date:** [August 3, 2017, 4:04am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/19 "2017-08-03T04:04:50Z")

</div>

@paulovitorjp i am calling loadChat function from constructor.  
And yes i am pushing new msgs to the array when new message is arrived from [socket.io](http://socket.io) or when user send message to other user.

And scrollBottom is called from \*ngFor because it isn’t working if i comment that code from \*ngFor so i can say for sure that this scrollBottom function is being called from \*ngFor.

This is my code when new message is arrived.

```auto
this.socket.on('sendMessage',(msgData)=>{

      var parsedData = JSON.parse(msgData);

      this.socket.emit('msgAcknowledgement',{
        id:parsedData.data.id
      });

      this.msgs.push({
        place:'left',
        profile:this.userDetail.userPic,
        msg:parsedData.data.message,
        msg_type : parsedData.data.msg_type,
        thumb:parsedData.data.thumb
      });
      this.scrollToBottom();

      this.sqlite.addItem({
        FromUser:parsedData.data.forid,
        toUser:parsedData.data.toid,
        Message:parsedData.data.message,
        msg_type:parsedData.data.msg_type,
        thumb:parsedData.data.thumb
      });

      //get current timestamp
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();

      this.events.publish('updateLastMessage',{
        toUser:parsedData.data.forid,
        message:parsedData.data.message,
        msg_type:parsedData.data.msg_type,
        thumb:parsedData.data.thumb,
        dateTime:year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
      });

    });

```

and this is code when user send new message

```auto
sendMessage(text)
  {
    if(text.trim() == ''){
      return false;
    }
    setTimeout(()=>{
      this.msgText = '';
      this.msgs.push({
        place:"right",
        profile:this.userImage,
        msg:text.trim(),
        thumb:'',
        msg_type:1
      });
      var dataToSend = {
        toUser:this.userDetail.uid,
        FromUser:this.userID,
        Message:text,
        thumb:'',
        msg_type:1 // 1:text,2:image
      };
      
      this.socket.emit('sendMessage',dataToSend);
      this.sqlite.addItem(dataToSend);

      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();

      this.events.publish('updateLastMessage',{
        toUser:this.userDetail.uid,
        message:text,
        msg_type:1,
        thumb:'',
        dateTime:year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
      });

      this.scrollToBottom();

      this.myInput.setFocus();
    },50);
    
  }

```

---

<div class="post-metadata">

**Author:** ![vishal\_tech](https://avatars.discourse-cdn.com/v4/letter/v/ecb155/32.png) [@vishal\_tech](https://forum.ionicframework.com/u/vishal_tech)\
**Post date:** [August 10, 2017, 7:13am UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/20 "2017-08-10T07:13:45Z")

</div>

Hello,

To open a page at bottom position

> ionViewWillEnter() {  
> setTimeout(() =\> {  
> this.content.scrollToBottom(0);  
> }, 100);  
> }

Using setTimeOut() methods you can achieve your solutions.

Thanks,

---

<div class="post-metadata">

**Author:** ![bilow](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bilow](https://forum.ionicframework.com/u/bilow)\
**Post date:** [August 10, 2017, 12:39pm UTC](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634/21 "2017-08-10T12:39:33Z")

</div>

> [@lilbiscuit](#):
>
> Is this possible with Ionic V1 ?

**Try this with v1:**

in your html

```auto
<div ng-repeat="m in messages track by $index" ng-init="scrollBottom();">
  Content bla bla: {{m.message}}
</div>

```

and in your controller (or create a directive)

```auto
app.controller('yourChatController', function($scope, $ionicScrollDelegate, $timeout) {
    $scope.scrollBottom = function(){
        $timeout(function () {
             $ionicScrollDelegate.resize();
             $ionicScrollDelegate.scrollBottom();
             // $ionicScrollDelegate.scrollBottom(true); can be used for a smooth scroll to the bottom
        }, 10); //to be safe using a 10 ms timeout on the scroll
    };
});

```

I didn’t try it with the newer versions in Ionic, but the general idea should work(?).

[Next page](https://forum.ionicframework.com/t/open-page-already-scrolled-at-bottom-chat-app/77634.md?page=2)
