[Angular] Scrolling the Message List To the Bottom Automatically Using OnChanges

Let‘s say the message list can Input (messages) from parent component, and what we want to do is when there are new message, we will scroll message list to the bottom to show the lastest message.

import {Component, Input, ViewChild, OnChanges, SimpleChanges} from ‘@angular/core‘;
import {last} from ‘lodash‘;

@Component({
  selector: ‘message-list‘,
  templateUrl: ‘./message-list.component.html‘,
  styleUrls: [‘./message-list.component.css‘]
})
export class MessageListComponent implements OnChanges{

  @Input() messages;

  @ViewChild(‘list‘) list;

  constructor() { }

  ngOnChanges(changes: SimpleChanges): void {
    if(changes[‘messages‘]) {
      const previous = changes[‘messages‘].previousValue;
      const current = changes[‘messages‘].currentValue;
      if(previous.length < current.length) {
        setTimeout(() => this.scrollToBottom());
      }
    }
  }

  scrollToBottom(): void {
    const items = this.list.nativeElement.querySelectorAll(‘li‘);
    const lastItem: any = last(items);
    lastItem.scrollIntoView();
  }
}
时间: 2024-12-22 06:18:22

[Angular] Scrolling the Message List To the Bottom Automatically Using OnChanges的相关文章

Commit message 和 Change log 编写指南

来源:http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html Git 每次提交代码,都要写 Commit message(提交说明),否则就不允许提交. $ git commit -m "hello world" 上面代码的-m参数,就是用来指定 commit mesage 的. 如果一行不够,可以只执行git commit,就会跳出文本编辑器,让你写多行. $ git commit 基本上,你写什么都

commitizen和cz-customizable配置git commit message

起因 团队对提交的commit message格式有约定俗称的要求,但是没有一个统一的规范,导致大家提交的commit message或多或少不太一样.因此,需要一个工具来帮助大家统一commit message的格式,也方便后续的分析和拓展. commitizen commitizen 是一个帮助规范commit message的工具.安装后的效果如下图: 安装commitizen npm install -g commitizen 安装adapter commitizen根据不同的adapt

git提交时写message的规范

message规范 angular示例 commit message(提交说明) git commit -m "写一行提交说明" # 跳出文本编辑器,写多行 git commit # add && commit 注意:add只针对修改文件不包括新增加的文件 git commit -am "" commit message格式 <type>(<scope>): <subject> // 空一行 <body>

格式化你的git message

https://github.com/angular/angular.js/blob/f3377da6a748007c11fde090890ee58fae4cefa5/CONTRIBUTING.md#commit Git Commit Guidelines We have very precise rules over how our git commit messages can be formatted. This leads to more readable messages that a

标准化 Git Message

使用Commitizen,标准化Git的Commit Message. AngularJS Commit 规范 Commit Message 格式 <type>(<scope>): <subject> <空行> <body> <空行> <footer> 释意 type feat:新功能 fix:修复问题 docs:文档 style: 格式 refactor:重构 test:增加测试 chore:构建过程或辅助工具的变动 s

Linux -- top (man)

TOP(1)                                                             User Commands                                                             TOP(1) NAME       top - display Linux processes SYNOPSIS       top -hv|-bcHiOSs -d secs -n max -u|U user -p p

Java Logging: Logger Hierarchy

Table of Contents Filters and Handlers in the Logger Hierarchy Log Levels of Loggers in the Hierarchy The Logger's used in your application are typically organized into a hierarchy, as mentioned elsewhere in this tutorial. This text will take a close

java script 实现像素小鸟

用java script 实现像素小鸟 <html部分> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>flybird</title> <link rel="stylesheet" type="text/css" href="css/bird.css"/> &

PrimeNG安装使用

PrimeNG是Angular2的一个富客户端组件,可以独立于Bootstrap单独使用,也可以结合Bootstrap共同使用. 官方上只有例子的介绍,并没找到个类似入门教程一类的资料,这里自己结合网上搜到的资料整理一份入门资料. 官网网址:https://www.primefaces.org/primeng/ 我使用angular-cli生成项目 需要安装的包有primeng.font-awesome @angular/animations看你项目之前是否安装过,如果没有则需要安装,否则会报错