简体   繁体   English

如何在 Angular CLI V12 中为 div 制作变量 css 高度

[英]How to make a variable css height for a div in Angular CLI V12

I'm fairly new to developing in Angular and I'd like some help with finding the best way to create a variable CSS height in Angular CLI V12.我对在 Angular 中进行开发还很陌生,我希望得到一些帮助,以找到在 Angular CLI V12 中创建变量 CSS 高度的最佳方法。 To simplify my question by a lot I'll present my problem as follows.为了简化我的问题,我将按如下方式提出我的问题。 I've got three boxes as seen below.我有三个盒子,如下所示。

Picture of boxes盒子的图片

These three boxes were made by three components.这三个盒子由三个组件制成。 The code of which is below.其中的代码如下。

app.component.html: app.component.html:

<app-top></app-top>
<app-center></app-center>
<app-bottom></app-bottom>

top component:顶级组件:

 .box { width: 500px; height: 100px; padding: 10px; border: 5px solid black; margin: 0; }
 <div class="box">top box</div>

center component:中心组件:

 .box { width: 500px; height: 100px; padding: 10px; border: 5px solid green; margin: 0; }
 <div class="box">center box</div>

bottom component:底部组件:

 .box { width: 500px; height: 100px; padding: 10px; border: 5px solid yellow; margin: 0; }
 <div class="box">bottom box</div>

What I want to do here is change the height of the center box dynamically through a variable in the typescript portion of my Angular project.我在这里要做的是通过我的 Angular 项目的 typescript 部分中的变量动态更改中心框的高度。 I don't think what I'm trying to do here is all that complicated but all the sources I find online for this are somewhat outdated and a bit confusing to follow if you are new to the development scene.我不认为我在这里尝试做的事情那么复杂,但是如果您是开发领域的新手,我在网上找到的所有资源都有些过时并且有点令人困惑。 What would be the best way to go about doing this? go 关于这样做的最佳方法是什么? Thank you.谢谢你。

You can just use ngStyles to add style to html tags.您可以只使用 ngStyles 将样式添加到 html 标签。 For eg:例如:

<div class="box" [ngStyle]="{'height': myCustomHeight}">center box</div>

Here "myCustomHeight" will be the variable whose value you can change accordingly in typescript.这里“myCustomHeight”将是您可以在 typescript 中相应更改其值的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM