css - 动态改变Angular中元素的宽度
问题描述
原谅我,我是 Angular 的新手。
我试图在单击按钮时动态更改 div。我试过[style.width]
和[style.width.px]
. 并且它最初似乎可以设置值,但是当我尝试在函数中更改它时,它似乎没有在屏幕上呈现。意思是,它在 html 中发生了变化,但在视觉上没有变化。我一直盯着这个太久了,所以一些 hep 将不胜感激。
html中的div
<div class='logo-contain' [style.width]='logoWidth'>stuff</div>
在 ts 文件中设置初始变量的位置
logoWidth = '450px';
以及ts文件中的函数
closeSideMenu(){
if(my conditional){
this.logoWidth = '50px;'
}else{
this.logoWidth = '250px;'
}
}
解决方案
您可以使用ngStyle
<div class='logo-contain' [ngStyle]='{ width : logoWidth }'>stuff</div>
甚至[style.width
需要工作,这;
可能会导致问题,因此请将其删除并尝试。
closeSideMenu(){
if(my conditional){
this.logoWidth = '50px'
}else{
this.logoWidth = '250px'
}
}
或者,您可以提供像素值绑定style.width.px
<div class='logo-contain' [style.width.px]='logoWidth'>stuff</div>
closeSideMenu(){
if(my conditional){
this.logoWidth = 50
}else{
this.logoWidth = 250
}
}
推荐阅读
- macos - egrep 返回由字符串分隔的匹配项
- security - Google SSO 是否只是简单地对用户进行身份验证,然后由 Web 应用程序/依赖方授权?
- angular - 如何将 angular/springboot 部署到天蓝色?
- arrays - bash在同一索引处索引多个不同的数组以进行循环
- django - 在 gunicorn 上运行时,在 django 应用程序中缓存数据的更好方法是什么
- rust - 线程“主”在“没有反应器运行,必须从 Tokio 0.2.x 运行时的上下文中调用”时惊慌失措
- ethereum - 您如何设置 Solidity 开发环境?
- swift - 如何摆脱:let id = UUID() is Immutable error inside decoded struct
- python - 如何将 Tkinter Gui 应用程序源代码分成多个文件
- html - 如何使 ActivePDF WebGrabber 为正文部分的长文本正确分解页面