回复 similar question ,已经有一年多了,我读到了一种使用 Dart(和 polymer Dart )中的数据绑定(bind)来禁用按钮的简单方法。
我当前的代码如下所示:
html:
...
<button id="btnPointDown" on-click="{{decrement}}" disabled="{{points == 0}}">\/</button>
...
。 Dart :
...
@published int points = 0;
void increment() {
points++;
}
void decrement() {
points--;
}
...
然而,Dart 似乎不再对禁用元素“聪明”了。
如何使用最新的 Dart 和 Polymer 来禁用使用数据绑定(bind)的按钮(或者如果不能以编程方式)?
最佳答案
绑定(bind)到 disabled 属性可以这样完成:
<button ... disabled?="{{ points == 0 }}">Content</button>
这个
?
是 Polymer 引入的特殊语法,用于支持绑定(bind)到这种 bool 属性。这不起作用:
<button ... disabled="{{ points == 0 }}">Content</button>
因为它会导致
<button ... disabled="false">Content</button>
这仍然会禁用该按钮。
对于 polymer >= 1.0 要使用的新语法是:
<button ... disabled$="{{value}}">Content</button>
注:
value
正如 Marco 在下面指出的那样,已经必须是一个 bool 值。否则,您必须创建一个返回 points == 0
的函数。 .见 Data Binding Documentation在这里和 Migration Guide这里供引用。问候,
罗伯特
关于dart - 如何在 Dart 中使用数据绑定(bind)禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819928/