因此,您的问题分为两个步骤。一种是计算页面高度,这不是角度字段,而是特定于浏览器的问题。尽管浏览器确实公开了clientHeight或scrollHeight属性,但是不同的浏览器如何理解页面的实际高度是有区别的。
所以最佳实践(著名的jQuery库也使用它)是获取不同的文档级高度属性并从中获取最大值。
您必须注入DOCUMENT常量才能将 DOM 文档访问到您的组件中。请找到以下示例角度组件代码。
import { Component, OnInit, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
public height = 0;
constructor(@Inject(DOCUMENT) private document: any){
}
ngOnInit() {
this.height = Math.max( this.document.body.scrollHeight, this.document.body.offsetHeight,
this.document.documentElement.clientHeight, this.document.documentElement.scrollHeight, this.document.documentElement.offsetHeight );
}
}
现在只需在 html 端,您就可以测试组件的height 属性并根据需要应用样式。有人这样想。。
<div [ngStyle]="{'height': height>=200 ? '200px' : '60px' }"></div>
谢谢。