我有一个 dom-repeat
元素,当它被渲染时,它只有一个 name
属性被填充,而 price
将由 ajax 响应填充:
<template is="dom-repeat" id="myId" items="{{arr}}">
<div>[[item.name]]</div>
<div>[[item.price]]</div>
</template>
问题是在收集到响应后,price
没有呈现。
我试过:
document.querySelector('#myId').render();
收到响应后,但不起作用。
编辑:
我可以做 this.set('arr.x.price', value)
其中 x
是我的对象在数组中的索引,但我的上下文是在一个不知道索引的函数中,除非我计算它,这并不难,但肯定有更好的方法吗?
最佳答案
数组更新方法
- 要么使用
this.set
来更新数组值。引用:https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#array-binding - 或更新所有数组值后,您可以执行
this.notifyPath('arr',this.arr)
。引用:https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path
如果您使用方法 1,那么您可以使用 observe 属性来监听子字段的变化。引用:https://www.polymer-project.org/1.0/docs/devguide/templates.html#filtering-and-sorting-lists
<template is="dom-repeat" id="myId" items="{{arr}}" observe="price">
如果您使用方法 2,那么 dom-repeat
应该在数组更新后自动刷新。
如果仍然不起作用,则使用 render()
方法强制呈现内容。引用:https://www.polymer-project.org/1.0/api/#dom-repeat:method-render
this.$.myId.render()
关于javascript - Polymer 1.0 - 更改 Dom 重复中未更新的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254132/