【发布时间】:2018-04-24 13:34:55
【问题描述】:
我这里有一个笨蛋 - https://plnkr.co/edit/fzNJ7FLYxWbLPoxtYpEw?p=preview
它是一个简单的 Angular 应用程序。
我正在使用 @ViewChild 和 nativeElement.offsetHeight 捕获 div 的高度
是否可以在组件的样式块中使用这个数字。
在我的示例中,我尝试过但将其注释掉。
@Component({
selector: 'my-app',
templateUrl: './src/app.html',
styles: [`
.blockTwo {
background: yellow;
//height: this.contentHeight+px;
height: 200px;
}
`]
})
=
import {Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './src/app.html',
styles: [`
.blockTwo {
background: yellow;
//height: this.contentHeight+px;
height: 200px;
}
`]
})
export class AppComponent implements AfterViewInit{
@ViewChild('content')
elementView: ElementRef;
contentHeight: number;
constructor() {
}
ngAfterViewInit() {
this.contentHeight = this.elementView.nativeElement.offsetHeight;
}
}
【问题讨论】:
-
您可以使用这种样式绑定语法:
[style.height.px]="contentHeight".
标签: angular