我有这样的东西:
<dom-module id="bar-foo">
<template>
<span class$="{{getState()}}">Bar Foo</span>
</template>
<script>
(function() {
class BarFoo {
beforeRegister() {
this.is = 'bar-foo';
this.properties = {
data: {
type: Object,
notify: true,
observer: '_updateData'
};
}
getState() {
if (this.data) {
return this.data.val > 0 ? 'positive' : 'negative';
}
}
}
Polymer(BarFoo);
})();
</script>
</dom-module>
现在,getState
函数只被调用一次,但data
属性每秒钟更新一次。当data
变化时,是否可以更新类$属性?
如果您希望每次data.val
变化时都计算getState
函数,您可以将其作为参数传递给函数。
<span class$="{{getState(data.val)}}">Bar Foo</span>
有关计算绑定的更多信息,请参阅文档