aurelia 绑定样式

示例

使用Aurelia绑定到浏览器本机style属性。如果使用字符串插值,则应使用css别名,以便样式在Internet Explorer中起作用。

样式字符串

export class MyViewModel {
  constructor() {
   this.styleString= 'color: #F2D3D6; background-color: #333';
  }
}
<template>
  <div style.bind="styleString"></div>
</template>

样式对象

export class MyViewModel {
  constructor() {
   this.styles= {color: '#F2D3D6', 'background-color': '#333'};
  }
}
<template>
  <div style.bind="styles"></div>
</template>

字符串插值

与上面的字符串绑定非常相似,这使您可以使用字符串插值来绑定到样式。如果任何值更改,它们将在视图中进行相应更新。

注意:为了与Internet Explorer兼容,我们使用别名css来绑定样式。这样可以确保在Internet Explorer中进行字符串插值。

export class MyViewModel {
    color = 'red';
    height = 350;
    width = 350;
}
<template>
    <div css="width: ${width}px; height: ${height}px; color:${color}">My Text</div>
</template>