html - 可以使用 Angular 4 中的插值将值添加到内联样式

标签 html css angular bootstrap-4

我正在自学angular 4,我想知道是否可以这样做:

<div class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-
valuemin="0" aria-valuemax="100" style="width {{ item.percent_position}}%;">
{{ item.percent_position }}</div>
</div>

我想要实现的是根据 {{item.percent_position}} 抛出的值扩展宽度

当我使用 [style]="width: {{ item.percent_position }}%;" 我得到这个错误:
未捕获错误:不支持引号评估! 声明:{{item.percent_position}}%;

我想要的是:progress bar at 58%

我用上面的代码得到的结果是这样的:

最佳答案

当您使用方括号时,您将绑定(bind)到一个表达式,因此建议您的解决方案不起作用,因为 Angular 期望这是可执行的 JS:

 [style]="width: {{ item.percent_position }}%;"

相比之下,下面的应该工作得很好:

 [style.width]="item.percent_position + '%' "

如果要绑定(bind)多个样式,可以使用 ngStyle 绑定(bind)到一个对象:

 [ngStyle]="{ 'width': item.percent_position + '%' }"

无论如何:如果您使用方括号,请确保绑定(bind)到它的是一个可执行表达式!

关于html - 可以使用 Angular 4 中的插值将值添加到内联样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662772/

相关文章:

javascript - 在 bootstrap 网格中将 div 包裹在大 div 周围

javascript - 语义 UI 根本不起作用

javascript - 在特定时间段内特定 div 上的微调栏

html - 打开我的网页时不会加载外部 css 文件

javascript - 如何在aosp浏览器中禁用缩放?

html - 具有 flexbox 属性的布局

javascript - 我应该在我的代码中使用什么分辨率来测试表格是否超出页面?

javascript - 如何为 Angular 应用程序组织类(class)?

node.js - 在Docker中构建Angular会抛出 “environment.prod.ts path in file replacements does not exist”

jquery - 如何用mysql实现无限滚动?