我正在尝试在 dom-repeat 中显示包含数组的对象。 数据结构如下: answerObject: {第一个问题: ["一", "二"], 第二个问题:[“三”,“四”]}
我在嵌套的 dom-repeat 中使用计算绑定(bind)将对象转换为数组,然后使用另一个 dom-repeat 来显示数组的内容。
<ul>
<template is="dom-repeat" items="[[_makeArray(answerObject)]]" as="question">
<li>[[question.key]]</li>
<ul>
<template is="dom-repeat" items="[[question.listOfAnswer]]" as="answer">
<li>[[answer]]</li>
</template>
</ul>
</template>
我创建了answerObject属性,如下所示:
answerObject: {
type: Object,
notify: true,
value: {firstQuestion: ["one", "two", "three"],
secondQuestion: ["four","five"] },
observer: '_answerChanged'
},
我尝试了所有不同的方法来观察对象或数组,但没有一个触发函数“_answerChanged”或“_makeArray”。
/* mutating object and then notifyPath */
this.set('answerObject.firstQuestion', ["newone"]);
this.notifyPath('answerObject.firstQuestion');
/* mutating array then notifySplices */
this.push('answerObject.secondQuestion',"six");
this.notifySplices('answerObject.secondQuestion', [
{index:3, removed: [], addedCount: 1, object: _this.answerObject.secondQuestion, type:'splice'}
]);
/* dirty check */
var array = this.answerObject.firstQuestion;
this.answerObject.firstQuestion=[];
this.answerObject.firstQuestion = array;
有什么建议吗?我错过了什么?谢谢。
最佳答案
观察者未触发,因为您的场景不受支持。
Primitive array items are not supported. This is because primitives (like number, string and boolean values) with the same value are represented by the same object.
当前推荐的解决方法(也在我之前的链接中说明)是,您只需用一个简单的对象包装原始值即可。
所以,而不是这个:
["one", "two", "three"]
你使用这样的东西:
[{ value: "one" }, { value: "two" }, { value: "three" }]
关于javascript - dom-repeat 中的 polymer 可观察对象未得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380872/