[英]Polymer 1.0 - Change Property Value Not Updated in Dom Repeat
I have a dom-repeat
element, and when this is rendered it only has a name
property filled out, and the price
will be populated by an ajax reponse: 我有一个
dom-repeat
元素,当它被渲染时它只填写了一个name
属性,并且price
将由ajax响应填充:
<template is="dom-repeat" id="myId" items="{{arr}}">
<div>[[item.name]]</div>
<div>[[item.price]]</div>
</template>
The problem is that the price
isn't rendered after the response is gathered. 问题是收集响应后不会呈现
price
。
I tried: 我试过了:
document.querySelector('#myId').render();
After the response has been received, but doesn't work. 收到回复后,但不起作用。
EDIT: 编辑:
I can do this.set('arr.x.price', value)
where x
is the index of my object in the array, but my context is inside a function that does not know the index unless I calculate it, which isn't hard, but surely there must be a better way? 我可以这样做
this.set('arr.x.price', value)
其中x
是我在数组中的对象的索引,但我的上下文是在一个不知道索引的函数内,除非我计算它,这是'很难,但肯定有更好的方法吗?
Array update approaches 阵列更新方法
this.set
to update the array values. this.set
更新数组值。 Ref: https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#array-binding this.notifyPath('arr',this.arr)
. this.notifyPath('arr',this.arr)
。 Ref: https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path If you use approach 1. Then you can have observe property to listen for changes to a sub fields. 如果使用方法1.那么您可以使用observe属性来监听子字段的更改。 Ref: https://www.polymer-project.org/1.0/docs/devguide/templates.html#filtering-and-sorting-lists
参考: 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">
If you use approach 2, then the dom-repeat
should refresh automatically after array update. 如果使用方法2,则
dom-repeat
应在阵列更新后自动刷新。
If it still does not work, then use the render()
method to force render of content. 如果它仍然不起作用,则使用
render()
方法强制渲染内容。 Ref: https://www.polymer-project.org/1.0/api/#dom-repeat:method-render 参考: https ://www.polymer-project.org/1.0/api/#dom-repeat: method-render
this.$.myId.render()
In order for the updated array to be rendered, you can update it and then set
the value via a clone. 为了呈现更新的数组,您可以更新它,然后通过克隆
set
值。 For example: 例如:
this.arr[0].price = 99;
let arr_clone = this.arr.slice(0);
this.set('arr', arr_clone);
If you update the array with this.push('myarray', {item}); 如果你使用this.push更新数组('myarray',{item}); then that will trigger Polymer's binding observers.
那将触发Polymer的绑定观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.