代码之家  ›  专栏  ›  技术社区  ›  JV Lobo

在Aurelia中使用two-style.bind

  •  1
  • JV Lobo  · 技术社区  · 6 年前

    我想用两个 style.bind 在奥雷利亚,但它不起作用。 我也可以只用一个 样式.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的作用是返回 font-weight 在CSS语法中:

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

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

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

    不起作用…也许是因为 重量 是保留字吗?

    所以,基本上,我想实现的是 字体加粗 与我的 source.weight 值,然后设置 font-style: italic; 如果旗子 is_italic 是真的。

    有什么想法吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Jesse Hosain Ahmed    6 年前

    您可以使用 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>
    

    如果您想阅读更多关于样式绑定的内容,我建议 this article .