javascript - 如何在 polymer 中使用 jQuery 动态更改数组?

标签 javascript jquery arrays polymer web-component

我有一些像这样的 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/

相关文章:

javascript - 如何以编程方式将图片上传到 Facebook?

javascript - 如何在innerhtml生成的文本框中插入自动完成jquery

java - 如何获取二维数组中行和列的长度?

c - 错误 : invalid type argument of unary '*' (have 'int' )

php - 从 PHP 中的多维数组中删除重复数组

javascript - 我没有从 module.exports 得到答案

javascript - Node - 如何编写音频文件

javascript - Char : 21007 处的 IE6 Javascript 错误

javascript - getJSON 只返回 [object Object],[object Object]

javascript - 在下拉菜单中搜索按钮 OnClick