无法从字典中删除元素

时间:2016-01-13 09:05:01

标签: javascript dictionary polymer

按照上一个问题:How get the value of this polymer 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>

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

研究

这确实有效:delete this.dict[0];(该元素将替换为null

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

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

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

1 个答案:

答案 0 :(得分:0)

您已尝试使用this.splice(path, start, deleteCount)
您必须按如下方式使用它:
this.splice("dict", index, 1)其中index指的是要移除的元素的位置。 有关推,弹,拼接和其他聚合物有用功能的更多信息here