【发布时间】:2018-06-26 13:41:54
【问题描述】:
我正在创建一个聊天应用程序,我需要在其中实现自动滚动到文本消息的底部。 div 应该在页面的初始加载和重新加载时完成。我创建了一个自定义指令来实现这一点。但是,这似乎不起作用。
指令:
import { Directive,ElementRef,AfterViewInit} from '@angular/core';
@Directive({ selector: '[scrollToBottom]' })
export class ScrollToBottomDirective implements AfterViewInit {
constructor(private element:ElementRef) {
console.log('scroll', this.element);
}
ngAfterViewInit(){
this.scrollToBottom();
}
scrollToBottom(){
if(this.element){
(this.element as any).nativeElement.scrollTop =(this.element as any).nativeElement.scrollHeight;
console.log('scroll', (this.element as any).nativeElement.scrollTop)
console.log('scrollHeight', (this.element as any).nativeElement.scrollHeight)
}
}
}
我哪里做错了?
【问题讨论】: