【问题标题】:Bind array data to element, and change the array将数组数据绑定到元素,并更改数组
【发布时间】:2016-01-08 03:02:29
【问题描述】:

将数据数组绑定到元素并在数组的任何部分发生更改时让该元素“自动更新”的模式是什么?

假设在脚本中我将一个数组绑定到app.game.gameData = [],然后将该数组绑定到一个元素

<template is="dom-bind" id="app">
...
  <game-mat screens={{game.gameData}}></game-mat>

我用dom-repeat 设置了我的game-mat 元素,它将值传递给子元素等。

但是当我向game.gameData 添加新项目时,我希望game-mat 更新。显示新行等。而且,我希望子元素因绑定设置而更改,例如如果我更改 game.gameData[3].value = 50,我希望它反映在该子元素中。

据我所知,唯一呈现的数组是创建的初始数组。

我想处理这些数据并将其反映在该元素列表中。有什么好办法吗?

(我很乐意改变我现有的模式,只是现在不知道如何解决这个问题......)

【问题讨论】:

  • 如何将值插入到数组中?你需要这样做 this.set() / this.push() polymer-project.org/1.0/docs/devguide/templates.html
  • 是的,我忽略了这一点!很好的收获,似乎就是这样。我使用“外部”(即非 Polymer)脚本中的查询选择器访问 Polymer 元素,可以在那里使用那些 .push() 方法。谢谢!输入答案,如果您愿意,我会接受)

标签: polymer-1.0


【解决方案1】:

使用 push、pop、splice、shift、unshift 方法更新您的数组。

例如将对象推送到 this.employees:

 this.push('employees', { first: 'Jack', last: 'Aubrey' });

或设置子元素

 this.set('employees.0.manager.type', 'engineer');

有关更多信息,您可以阅读用于阵列的聚合物指南 https://www.polymer-project.org/1.0/docs/devguide/templates.html

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 2011-07-21
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2016-11-19
    相关资源
    最近更新 更多