我在转发器中有一个HTML元素,我想根据四个条件更改类。该条件基于JSON的属性。该属性名为type
,我正在使用的css类是red
,blue
和green
。条件是:
type
=红色,则分配css类red
type
=蓝色,则分配css类blue
type
=绿色,则分配css类green
type
或为null,则分配css类red
由于我对AngularJS的经验有限,我知道我可以使用ng-style
属性设置条件CSS。我如何使用Aurelia实现这一目标?
示例HTML标记
<div repeat.for="item of projects" class="col-lg-6">
<div class="card-header">
<!-- I want to change the css class red based on the value of the JSON attribute type -->
<div class="component-type red">
...
</div>
</div>
...
</div>
答案 0 :(得分:16)
<div repeat.for="item of projects">
<div class="${item.type || 'red'}">...</div>
</div>
在这里查看Ninja Turtle的工作要点:https://gist.run/?id=3a588b94a6b8d12666ba4f64645270aa
答案 1 :(得分:2)
您可以这样做:
<div repeat.for="item of projects" class="col-lg-6">
<div class="card-header">
<!-- I want to change the css class red based on the value of the JSON attribute type -->
<div class="component-type ${ item.type == undefined ? 'red' : item.type}">
...
</div>
</div>
...
或者您可以创建一个值转换器:
export class TypeColourValueConverter {
toView(value) {
let colour = value;
if (value === undefined) {
colour = red;
};
return colour;
}
}
然后您可以这样使用:
<div repeat.for="item of projects" class="col-lg-6">
<div class="card-header">
<!-- I want to change the css class red based on the value of the JSON attribute type -->
<div class.bind='item.type | TypeColour'>
...
</div>
</div>