发送消息或在 angular2 中打开消息后,如何使页面自动向下滚动到底部

Bhr*_*jni 2 typescript angular

我有一个消息对话部分,在那里我需要将滚动条显示到底部,当页面再次打开时,滚动条必须在底部。我的意思是应该首先显示最后一条消息。

HTML:

<ul>
        <li *ngFor="let reply of message_show.messages">
          <img [src]="reply.from_user_image || '../assets/images/msg.png'"/>
          <p><b>{{reply.name}} </b> <span> {{reply.updated_at | date:'dd.MM.yyyy'}} - {{reply.updated_at | date:'h:mm'}}</span></p>
          <p>{{reply.text}}</p>
        </li>
      </ul>
Run Code Online (Sandbox Code Playgroud)

:

loadMessages() {
    this.service
          .getMessages()
          .subscribe(
            data => {
              this.messagesdata = data;
              this.activeMessages = data.filter(msg => msg.active == true && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0)
              this.closedMessages = data.filter(msg => msg.active == false && msg.from_user_name !== 'Anonymus' && msg.messages.length > 0);
              if (this.activeMessages.length > 0) {
                if(!this.message_show) {
                  var test = this.message_show = this.activeMessages[0];
                  this.message = true;
                  this.message_id = this.activeMessages[0].id;
                  this.message_show.messages.map(function(msg) {
                    if(msg.from_user_id == test.from_user_id) {
                      msg.from_user_image = test.from_user_image;
                    } else {
                      msg.from_user_image = test.to_user_image;
                    }
                    if(msg.to_user_id == test.to_user_id) {
                      msg.to_user_image = test.to_user_image;
                    } else {
                      msg.to_user_image = test.to_user_image;
                    }
                  })
                }
              }             
            },error => {});
  }
Run Code Online (Sandbox Code Playgroud)

Sra*_*van 6

这是一个有角度的解决方案:

  • 我添加了#scrollCottom模板变量。
  • 您可以使用ViewChild来获取 Element 参考,并应检查滚动底部问题。

成分;

import { AfterViewChecked, ElementRef, ViewChild, OnInit} from 'angular2/core'
@Component({
    ...
})
export class YourComponent implements OnInit, AfterViewChecked {
    @ViewChild('scrollBottom') private scrollBottom: ElementRef;

    ngOnInit() {
        this.scrollToBottom();
    }

    ngAfterViewChecked() {        
     this.scrollToBottom();        
    } 

    scrollToBottom(): void {
        try {
            this.scrollBottom.nativeElement.scrollTop = this.scrollBottom.nativeElement.scrollHeight;
        } catch(err) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<ul #scrollBottom>
  <li *ngFor="let reply of message_show.messages">
    <img [src]="reply.from_user_image || '../assets/images/msg.png'"/>
    <p><b>{{reply.name}} </b> <span> {{reply.updated_at | date:'dd.MM.yyyy'}} - {{reply.updated_at | date:'h:mm'}}</span></p>
    <p>{{reply.text}}</p>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)


Lak*_*nna 5

您可以使用

 window.scrollTo(0,document.body.scrollHeight);
Run Code Online (Sandbox Code Playgroud)

在这里您可以找到相关讨论

自动滚动到页面底部