聚合物1.0:绑定css类不更新



我有这样的东西:

<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>

有关计算绑定的更多信息,请参阅文档

最新更新