聚合物选择值未设定

时间:2015-12-10 16:27:39

标签: javascript select nested polymer

我已经设置了一个JSbin来显示我在聚合物选择框中没有反映模型的问题(最初),但是,选择一个选项后模型会更新。请注意,在我的示例中,我确保在填充选择选项后加载模型,但是,我想这里的棘手问题是选择框填充是嵌套的dom-repeat(因此当填充模块时,必须重新填充' select'的嵌套dom-repeat - 我在猜测。

这里是垃圾箱,以及如何测试它以显示困境。

http://jsbin.com/xucavi/edit?html,console,output

页面加载后,请注意2个选择框中没有选择。点击'显示模型中的值'按钮。请注意,值显示模型具有值。选择' Digital'对于2个选择框中的每一个。现在点击'显示模型中的值'按钮再次 - 并注意模型已更新。

有关如何解决此问题的任何想法?感谢

1 个答案:

答案 0 :(得分:1)

您需要计算selected元素的option属性的值。

修改外部dom-repeat以将实际图书绑定到属性book而不是item。现在,将selected属性绑定到_computeSelected(item, book)函数可以动态计算所选的图书类型:

<option value='{{item.id}}' selected$='[[_computeSelected(item, book)]]'>{{item.type}}</option>

函数本身的实现非常简单:

_computeSelected: function(item, book) {
    return item.id === book.typeId;
}

你的JS Bin的工作示例: http://jsbin.com/sodugigubo/edit?html,console,output

如需更深入的解释,请参阅computed bindingattribute binding

警告:请注意Polymer dom repeat is not working for Select Option in IE