如何在Aurelia repeat.for中有条件地添加或删除CSS类?

时间:2015-12-07 11:19:50

标签: aurelia aurelia-framework

我有一系列的东西,我正在构建一个<select>,我希望能够使用CSS将first项标记为已禁用,但是我无法正确使用语法。

这就是我所拥有的:

<select select2 value.bind="selectedItem">
    <option repeat.for="item of stuff" model.bind="item" class="${${first} ? 'disabled selected hidden' : ''">
        ${item.key}
    </option>
</select>

HEREPlunker足够相似,可用作试验台。

我错过了什么?

1 个答案:

答案 0 :(得分:17)

你的例子不完整,但我想它应该是这样的:

class="${item.first ? 'disabled selected hidden' : ''}"

此外,如果您在VM上有first属性,例如stuff,则会写:

class="${$parent.first == item? 'disabled selected hidden' : ''}"

更新:

Plunker(http://plnkr.co/edit/2xywp0

<option repeat.for="thing of things" model.bind="thing">${thing.name} ${$first | stringify}</option>

您的语法错误:class="${${first} ? 'disabled selected hidden' : ''"应为class="${ $first ? 'disabled selected hidden' : '' }"

来自Aurelia docs:

Contextual items availabe inside a repeat template:

$index - The index of the item in the array.
$first - True if the item is the first item in the array.
$last - True if the item is the last item in the array.
$even - True if the item has an even numbered index.
$odd - True if the item has an odd numbered index.