我有以下情况:一种自定义元素,用于呈现阵列中的项目
<dom-module id="shopping-cart">
<template>
<style>
:host {
display: block;
}
</style>
<template is="dom-repeat" items=[[cartItems]]>
<tr>
<td>[[item.name]]</td>
<td><span>[[item.quantity]]</span> <small>[[item.uom]]</small></td>
<td><span>[[item.cost]]</span> <small>lei</small></td>
</tr>
</template>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'shopping-cart',
properties: {
cartItems: {
type: Object,
value: []
}
}
});
})();
</script>
<!-- And this is the container -->
<template is="dom-bind" id="app">
<shopping-cart cart-items="[[cartItems]]"></shopping-cart>
</template>
我正在根据一些事件操作app.cartItems
对象。我希望在更改数组时绑定会更新,但没有。当推送新元素或删除其他元素时,这些更改不会反映在shopping-cart
元素中。我尝试手动启动cart-items-changed
事件,但仍然无济于事。导致绑定刷新的唯一原因似乎是为app.cartItems
属性分配一个新数组,但这似乎不对。我觉得我错过了一些简单的步骤,但我不知道是哪一个。有什么想法吗?
参见
- https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#array-装订
- https://www.polymer-project.org/1.0/docs/devguide/properties.html#array-突变
每个聚合物元件都有以下阵列突变方法可用:
- push(路径,项目1,[…,项目N])
- pop(路径)
- unshift(路径,项1,[…,项N])
- 移位(路径)
- 拼接(路径,索引,removeCount,[item1,…,itemN])
您需要为Polymer使用其中一个来获得有关更改的通知。