【发布时间】:2018-08-29 19:16:54
【问题描述】:
我一直在寻找解决问题的方法,遇到了 this post 和 this other post,但它并没有解决我的问题。
我想动态地将<li> 添加到<ul>,但是当我使用数组变异方法时它不起作用(我可以正确记录数组的更新)。
我尝试使用 JSON 数组而不是简单字符串,并尝试执行 this.notifyPath('peoples');,但仍然无法正常工作
static get properties() {
return {
peoples: {
type: Array,
value: []
}
}
}
_test2() {
console.log(this.peoples);
}
_test() {
this.push('peoples', 'test');
}
<div class="flex__item--top list">
<ul id="namelist">
<template is="dom-repeat" items={{peoples}}>
<li>{{item}}</li>
</template>
</ul>
</div>
【问题讨论】:
-
首先,使用临时数组将一些元素推送到数组中。然后使用
this.set('people', tempArray),这将在people数组上产生可观察到的变化,并将立即在dom-repeat生效。
标签: javascript html dom polymer-2.x