无法从字典中删除元素 [英] Can't remove element from dictionary

查看:109
本文介绍了无法从字典中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的问题之后:如何获取此聚合物元素的值?,当我将此自定义元素导入另一个自定义元素时,需要从作为属性传递的字典中删除一个元素.

Following my previous question : How get the value of this polymer element ?, I need to remove an element from a dictionary that I pass as an attribute when I import this custom element in another custom element.

流元素

<script>
Polymer({
  is: 'flow-element',
  properties: {
    dict: {
      type: String,
      notify: true
    },
    name: {
      type: String
    },
    kind: {
      type: String
    }
  },

  handleClick: function() {
    console.log('clicked: ' + this.name);
    // the output does work
    console.log('Dict before ::' + JSON.stringify(this.dict, null, 4));

    // this does NOT work
    delete this.dict[this.name];

    // the output does work, but dictionary is unchanged
    console.log('Dict after ::' + JSON.stringify(this.dict, null, 4));

  }
});
</script>

流列表

<flow-element dict={{flowDictionnary}} name="{{item.first}}" kind="{{item.last}}"></flow-element>

我可以访问字典(打印其内容),但是由于某种原因,我无法从字典中删除任何项目.

I am able to access the dictionary (print its content) but for some reason, I can't remove any item from it.

研究

这确实有效:删除this.dict [0]; (元素被替换为 null )

This does work : delete this.dict[0]; (well the element is replace with null)

之前

{
    "first": "Bob",
    "last": "Smith"
}
{
    "first": "Sally",
    "last": "Johnson"
}

之后

null,
{
    "first": "Sally",
    "last": "Johnson"
}

但是,显示字典元素的列表不会更新,并且元素会保留在屏幕上.

However, the list that displayed the dictionary element does not update, and element stay on the screen.

推荐答案

您尝试使用 this.splice(path,start,deleteCount)?
您必须按以下方式使用它:
this.splice("dict",index,1),其中 index 是指要删除的元素的位置.

http://polymer.github.io/polymer/"rel =" nofollow>此处.

You've tried to use this.splice(path, start, deleteCount)?
You have to use it as follows:
this.splice("dict", index, 1) where index refers to the position of the element to remove. More information about push, pop, splice and other polymer's useful functions here.

这篇关于无法从字典中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆