【问题标题】:Polymer not removing deleted element聚合物不去除已删除的元素
【发布时间】:2016-01-13 16:55:49
【问题描述】:

使用 Polymer Stater 套件,我创建了两个自定义元素:

  • flow-list.html(这里声明为 flowElementArray
  • flow-element.html(这里定义了删除函数)

app.js文件中,我还定义了一个addElement函数。

我可以将元素添加到 flowElementArray,然后它们就会显示出来。

但是,当我从 flowElementArray 中删除元素时,它们仍然会显示出来。

这是我得到以下结果的方式:

  1. 应用启动(预加载 2 项)
  2. 我删除了一个项目(该项目停留在屏幕上)
  3. 我添加一项(添加的项,在删除的上面顺便说一句)

这种奇怪行为的根源是什么?

编辑我无法让示例在 plunker/codepen.io/jsbin 上运行,所以它在 github 上。

如何添加元素:

app.storageLoaded = function() {

  if (this.$.s1.value === '' || this.$.s2.value === '') {
    window.alert('One field is Empty!');
    return;
  }

  this.$.flowListID.push('flowDictionnary',
  {
    first: this.$.s1.value,
    last: this.$.s2.value
  });
};

我如何删除:

removeItem: function() {
  var counter = 0;
  while (counter < this.dict.length) {
    var item = this.dict[counter];
    if (item.first === this.name) {
      this.dict.splice(counter, 1);
    } else {
      counter++;
    }
  }
}

【问题讨论】:

  • 向我们展示代码,以便我们为您提供帮助。如果你在 jsbin 中做就完美了。
  • 我在这种网站(jsbin、codepen、plunker)上没有成功地从 Polymer 制作一个简单的自定义元素,因此我决定将我的工作上传到 GitHub 并创建一个 GitHub。我知道它不如这些网站好。至少它(不)完全可以在我的电脑上工作。
  • 没有人有兴趣调查整个项目。请提供一个允许重现问题的最小示例(另请参阅sscce.org
  • 您需要阅读以下内容:polymer-project.org/1.0/docs/devguide/…。听起来您没有使用 this.push()this.splice() 从数组中添加/删除项目。
  • 我添加了添加和删除元素的方式。我正在研究一个 jsfiddle。

标签: javascript data-binding polymer polymer-1.0


【解决方案1】:

在Polymer中对数组属性进行操作时,需要使用this.push('myArrayProperty', item)this.splice('myArrayProperty', 0, 1)或在操作后调用this.notifyPath('myArrayProperty')

removeItem: function() {
  var counter = 0;
  while (counter < this.dict.length) {
    var item = this.dict[counter];
    if (item.first === this.name) {
      // this.dict.splice(counter, 1);
      this.splice('dict', counter, 1);
    } else {
      counter++;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多