Angular:element.Scrolltop = element.scrollheight不起作用
创始人
2024-11-01 21:00:28
0

在Angular中,你可以使用ViewChild来获取到DOM元素的引用,然后使用该引用来设置scrollTop属性。以下是一个示例解决方法:

在组件的HTML模板中,给要滚动的元素添加一个本地引用:

在组件的Typescript文件中,使用ViewChild获取到该元素的引用,并在需要的时候设置scrollTop属性:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponentComponent implements AfterViewInit {
  @ViewChild('scrollContainer') scrollContainer: ElementRef;

  ngAfterViewInit(): void {
    // 在视图初始化后设置scrollTop属性
    this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
  }
}

上述代码中,我们使用ViewChild来获取到名为scrollContainer的元素的引用。然后在ngAfterViewInit生命周期钩子中,我们设置scrollTop属性为scrollHeight。这将使元素自动滚动到底部。

请确保在使用ViewChild之前,已经导入了ViewChild,ElementRef和AfterViewInit。

相关内容

热门资讯

透视妙招!aapoker辅助软... 透视妙招!aapoker辅助软件合法吗(透视)都是真的是有辅助辅助器(哔哩哔哩)1、aapoker辅...
透视要领!wepoker挂(透... 透视要领!wepoker挂(透视)都是是有辅助下载(哔哩哔哩)1、进入游戏-大厅左侧-新手福利-激活...
透视经验!wepoker透视是... 透视经验!wepoker透视是真的吗(透视)果然真的有辅助神器(哔哩哔哩)wepoker透视是真的吗...
透视秘籍!wepoker透视脚... 透视秘籍!wepoker透视脚本免费app(透视)一直存在有辅助工具(哔哩哔哩)1、超多福利:超高返...
透视模块!德州之星扫描器(透视... 透视模块!德州之星扫描器(透视)都是有辅助辅助器(哔哩哔哩)运德州之星扫描器辅助工具,进入游戏界面。...
透视步骤!wepoker怎么获... 透视步骤!wepoker怎么获得好牌(透视)都是存在有辅助软件(哔哩哔哩)1、金币登录送、破产送、升...
透视法门!福建大菠萝万能辅助器... 透视法门!福建大菠萝万能辅助器(透视)都是是真的辅助软件(哔哩哔哩)1、透视法门!福建大菠萝万能辅助...
透视要领!pokeplus脚本... 透视要领!pokeplus脚本(透视)本来有辅助安装(哔哩哔哩)1、pokeplus脚本透视辅助软件...
透视项目!wepoker游戏下... 透视项目!wepoker游戏下载(透视)其实是有辅助神器(哔哩哔哩)1、超多福利:超高返利,海量正版...
透视诀窍!wepoker透视方... 透视诀窍!wepoker透视方法(透视)竟然存在有辅助插件(哔哩哔哩)小薇(辅助器软件下载)致您一封...