设计一个聊天网页,需要把显示聊天内容的div自动滚屏到底部,但是div缺少scrollTo函数,变通的方法是
div.scrollTop = div.scrollHeight
但在angular中,如何找到这个时机呢? ngFor执行完毕是个时机,但ngFor语句没有提供finished事件。这个事件只能自己制造。
...
{ {ngForCallback()}}
component中增加ngForCallback方法
public ngForCallback() {
...
}
推荐使用AfterViewChecked and @ViewChild 方法,下面详细介绍:
在component中:
import {..., AfterViewChecked, ElementRef, ViewChild, OnInit} from 'angular2/core'
@Component({
...
})
export class ChannelComponent implements OnInit, AfterViewChecked {
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
//ngOnInit() {
// this.scrollToBottom();
//}
ngAfterViewChecked() {
this.scrollToBottom();
<