javascript - dom-repeat 中的 polymer 可观察对象未得到更新

标签 javascript dom polymer

我正在尝试在 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;

有什么建议吗?我错过了什么?谢谢。

最佳答案

观察者未触发,因为您的场景不受支持。

根据 official documentation :

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/

相关文章:

javascript - 如何将 DOM 元素扩展为一个类(不使用 jQuery)

dart - Dart Polymer分芯器:拖动/调整大小事件

javascript - 在带有数字的文本框中自动添加逗号 (,)

javascript - 未捕获的类型错误 : Cannot read property 'substr' of undefined

javascript - 将 UL 转换为适用于移动设备的 SELECT,然后在调整大小时再次返回

php - DOM 错误 - ID 'someAnchor' 已在实体中定义,第 X 行

javascript - Tinymce IE6+ 如何获得工作范围?

javascript - 在哪里编写 Polymer 的处理函数

Dart polymer : unable to get input from array

c# - 如何将 Angular POST 发送到 C# Asp.Net MVC Controller ?