我有一些像这样的 HTML
<iron-list is="dom-repeat" items="[[userList]]" as="user">
<paper-checkbox class="styled sizeCheckbox">
<div class="primary">[[user.userName]]</div>
</paper-checkbox>
</iron-list>
在我的 js 文件中,我正在更新“userList”数组。
if (event.target.checked) {
this.userList.push(event.target.value);
} else {
var index = this.userList.indexOf(event.target.value);
this.userList.splice(index, 1);
}
当我更新数组中的值时,它并没有直接反射(reflect)“userList”数组中的值,我们如何在不刷新页面的情况下更新它?
最佳答案
使用 Polymer 的数组突变方法对数组进行可观察的更改。 https://www.polymer-project.org/1.0/docs/devguide/model-data#work-with-arrays
请更新您的代码:
if (event.target.checked) {
this.push('userList', event.target.value);
} else {
var index = this.userList.indexOf(event.target.value);
this.splice('userList', index, 1);
}
我希望这个Demo可以帮你。
关于javascript - 如何在 polymer 中使用 jQuery 动态更改数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45025636/