javascript - Polymer.prototype.splice 的行为不符合预期

标签 javascript polymer

我正在使用 Polymer 的数组突变方法进行数组突变。

  this.allRules = [{name: "abc", enabled: true}];     

  var item = this.allRules[index];
  item.name = "abcdxfdsa";
  item.enabled = enabled;

  // console log out [Object]
  console.log(this.allRules);
  this.splice('allRules', index, 1);
  // Instead of logging out [], it logs out [splices: Object]
  console.log(this.allRules);
  this.push('allRules', item);
  // It logs out [Object, splices: Object]
  console.log(poly.allRules);

令人烦恼的是这个。当我将 this.allRules 绑定(bind)到 dom-repeat 模板(如下所示)时,更新绑定(bind)数据时,它会显示拼接的项目,而不是新添加的项目元素。因此,在拼接和推送之后,我得到的不是 "abcdxfdsa" 作为项目名称,而是 abc,这是拼接之前的值。以下是示例 dom-repeat 数据绑定(bind)的链接。 Polymer dom-repeat how to notify array updated

最佳答案

实际上应该从数组中删除该项目。第二次它以 [splices: Object] 身份注销,只是为了显示有关此数组的更多信息,因为它有一个名为 splices 的属性。 。如果您在 Firefox 中运行相同的内容,您仍然会看到 [object] 所以我猜这只是 Chrome 控制台助手的东西。

要获取要更新的值,一种方法是将 this.push 调用包装在 this.async 方法中。

  this.async(function() {
    this.push('allRules', item);
    console.log(item);
    console.log(this.allRules[0].name);
    // returns 1, meaning it's filled with one item again
    console.log(this.allRules.length);
  });

看看这个 plunker一个工作样本。

关于javascript - Polymer.prototype.splice 的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32082760/

相关文章:

javascript - 从回调访问 polymer 组件属性?

javascript - JS 切换动画(animate.css)?

php - 为什么这个 Javascript 在 Firefox 和 Explorer 中运行一次,而在 Opera 中运行 3 次?

javascript - 按数组索引进行颜色分组,Javascript

javascript - 脚本在浏览器中工作,但在 NodeJS 中不工作

html - 用 polymer 制作垂直纸标签

javascript - jquery 复选框多重禁用

dart - 嵌套和模板if =样式的 polymer Dart 元素的构造和排序?

javascript - Polymer 1.0 中的路由

Polymer CLI 构建内存不足