如何在Angular 4中设置元素的动态高度?

时间:2018-11-22 07:17:42

标签: css angular

我无法使用Angular 4打字稿动态设置Div元素的高度。

预先感谢

3 个答案:

答案 0 :(得分:1)

使用具有您要输入的高度的变量...,然后使用[ngStyle] =“ {'height':[yourVariableName]}”

答案 1 :(得分:0)

  1. 您可以为两个类添加样式,例如 .class1 {height:20px} .class2 {height:40px}

对于div,添加[class] =“ ifSomething?'class1':'class2'”

  1. 使用ElementRef.nativeElement.getElementByClassName并通过js更改div的样式。

答案 2 :(得分:0)

可以通过以下方式使用 [style.height]

 //shouldExpand  is a condition, you can replace it with  your condition
<div class="yourClass"
[style.height]="shouldExpand === true ? '100px' : '70px'">