css - 在 Aurelia 中使用两个 style.bind

标签 css aurelia

我试图在 Aurelia 中使用两个 style.bind 但它不起作用。 我也可以只使用一个 style.bind 并应用我想要的样式,但我不知道该怎么做。

这是一段代码:

<span repeat.for="source of item.data.sources | sort: 'weight' : 'asc'" 
  if.bind="source.weight" 
  class="weight" 
  style.bind="source.weight | fontWeight" 
  style.bind="source.is_italic && 'font-style: italic;'"
>
  ${source.name}
</span>

我的 fontWeight valueConverter 唯一做的就是返回 CSS 语法中的 font-weight:

export class FontWeightValueConverter {
  toView(weight) {
    return 'font-weight: ' + weight;
  }
}

我必须这样做,因为这样做

style="font-weight: ${ source.weight }"

不起作用...可能是因为 weight 是保留字?

所以,基本上,我想要实现的是将 font-weight 设置为我的 source.weight 值,然后设置 font-style: italic; 如果标志 is_italic 为真。

有什么想法吗?

最佳答案

您可以为此使用 css 属性。您可以在 css 属性的值中输入字符串内插样式,以允许您创建所需的行为。

在你的情况下,你需要这样的东西:

<span repeat.for="source of item.data.sources | sort: 'weight' : 'asc'" 
  if.bind="source.weight" 
  class="weight" 
  css="font-weight: ${source.weight}; ${source.is_italic ? 'font-style: italic;' : ''}"
>
  ${source.name}
</span>

如果您想阅读更多关于样式绑定(bind)的内容,我推荐 this article .

关于css - 在 Aurelia 中使用两个 style.bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441097/

相关文章:

Aurelia 验证js

javascript - 如何清除 aurelia 中的文件输入绑定(bind)

javascript - aurelia-dialog modal 的 body 大小不正确

javascript - 如何使用 html css js 生成随机大小的数组以对可视化器进行排序?

unit-testing - 在 Internet Explorer 中运行 Karma 时在 aurelia-testing 中使用 WaitForElement

php - 文件未上传并出现错误 "File is not uploaded please try again"?

css - 自定义 Bootstrap 复选框

javascript - Aureliajs 在 App Constructor 上等待数据

html - 网页上的悬停效果覆盖下拉导航栏

jquery - 通过 ID 动态选择嵌套元素