您的位置:首页 > 其它

Polymer1.0中动态设置disabled

2015-10-22 14:08 260 查看
我想在下面这个button上动态绑定一个disabled,是的这个button在满足某些条件时显示,不满足时隐藏。

<paper-button class="green approveButton" on-click="reviewArt" data-version="{{item.version}}">Approve</paper-button>


问题就在于disabled是一个boolean,不是字符串。所以当我第一开始写成
disabled="{{item.disabled}}"


的时候,这个button不论item.disabled是true还是false都会显示出来。

后来在网上搜到这个http://stackoverflow.com/a/23822215/2177408

里面提了两种解决方法,一种是disabled?="{{item.disabled}}",一种是disabled$="{{item.disabled}}"。经过测试,只有第一种有效。
http://stackoverflow.com/a/23822215/2177408
Binding to the disabled attribute can be done like this:
<button ... disabled?="{{ points == 0 }}">Content</button>


This
?
is
special syntax introduced by Polymer to support binding to this kind of boolean attributes.

This does not work:
<button ... disabled="{{ points == 0 }}">Content</button>


Because it would result in
<button ... disabled="false">Content</button>


which would still disable the button.

For Polymer >= 1.0 the new syntax to use is:
<button ... disabled$="{{value}}">Content</button>


Note:
value
already
has to be a boolean as Marco pointed out below. Otherwise you have to create a function that would return
points
== 0
. See Data Binding Documentation here
and Migration Guidehere for reference.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: