Polymer1.0中动态设置disabled

来源:互联网 发布:sql注入 and 1 1 编辑:程序博客网 时间:2024/05/13 15:52


我想在下面这个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.




0 0
原创粉丝点击