html - 如何在Angular中更正以下表达式
问题描述
我想在 ngStyle 中设置动态宽度。我怎样才能正确地写出表达式。
[ngStyle]="{{'width': 'calc(chargeStation.response[1].width+'%' - 1+'px')' }}"
我已经改为
[ngStyle]="{'width': 'calc('+ chargeStation.response[1].width +'%'+ ')'}"
比在 HTML 中它作为对象
ng-reflect-ng-style="[object Object]"
解决方案
您可以尝试以下解决方案,它对我有用。
在模板文件 (.html) 中
<button [ngStyle]="{'width': 'calc(' + chargeStation.width + '% - 1px)'}" (click)="buttonClick()">click</button>
在组件文件 (.ts) 中
你必须在 ts 文件中为你的对象赋值例如我有带有 width 属性的 chargeStation 对象。属性值 100。
chargeStation = {
width: 100
};
推荐阅读
- django - 值错误:字段 'id' 需要一个数字,但得到了 'favicon.ico
- javascript - 为什么动画不适用于所有类元素?
- python - 让我的 Discord 机器人响应某个人
- flutter - 任务':audioplayers:compileDebugKotlin'的Flutter执行失败
- mysql - 求和类似值 MySQL 后每种类型的最大值
- reactjs - 在 React 中触发 bootstrap 5 模态确认
- flutter - 制作发布 apk 或在发布模式下运行应用程序时出错
- java - 将颜色的 xml 整数数组转换为字符串数组返回空值 - 如何以编程方式将其转换为十六进制?
- python - 11 个具有重复代码行的函数 - 有没有办法减少冗余?
- python - 不能再用 python 2.7 安装 pip 了?