使& # 39;dom-if& # 39;根据另一个元素中的属性检查条件

Make 'dom-if' check the condition against a property in another element?

本文关键字:属性 条件 检查 dom-if 另一个 元素      更新时间:2023-09-26

我有一些代码看起来像这样:

..
<template is="dom-if" if="_myMethod()">
   <div>Hello world</div>
</template>
..

_myMethod看起来像这样:

_myMethod: function(){
   return this.$.someOtherObject.someList.size() == 0;
}

可以看到,该方法返回一个来自页面中其他polymer-element的值。但是,当我的其他 polymer-element发生变化时,我如何让dom-if改变它的"状态"?

我知道,如果我传递一个值给方法,像这样:_myMethod(someValue),如果someValue改变,它将更新dom-if,但我需要"观察"另一个polymer-element的变化。我怎么做我想做的事?

经过一些实验,我最终得到了以下内容:

..
<my-other-element on-some-list-changed="_notifyChange" id="someOtherObject">
</my-other-element>
..
..
<template is="dom-if" if="_myMethod(didChange)">
   <div>Hello world</div>
</template>
..
..
_notifyDidChange: function()
{
   this.didChange = !this.didChange;
}
_myMethod: function(didChange){
   return this.$.someOtherObject.someList.size() == 0;
}

如果另一个元素上的值(数组)发生变化,dom-if现在将更新。不确定是否有一个错误(与数组),但我尝试绑定到someOtherObject.someList直接和同步两个元素之间的变化,他们同步,但事件没有正确触发,因此dom-if不会更新。(