以编程方式触发聚合物数据绑定更改?

时间:2016-01-07 11:45:18

标签: javascript polymer web-component

我正在使用Polymer的数据绑定来显示结构化数据。单击按钮后,我想更改数据项,但显示的driver.findElement(By.xpath("//p[@style='text-align: center;']/em/strong[text()='TEST']") 不会更改。

以下示例中我做错了什么?

我尽可能地简化了!

{{item.name}}

1 个答案:

答案 0 :(得分:1)

'items.0.name'中使用'items#0.name'代替repeater.set(){的预定义items数组中#repeater不匹配。这是一个有效的演示。



<html>  
<head>

  <title>Template me</title>
  
  <script src="https://rawgit.com/webcomponents/webcomponentsjs/master/webcomponents.js"></script>
  
  <base href="https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/">
  <link rel="import" href="polymer/polymer.html">  
  <link rel="import" href="paper-button/paper-button.html">  

  
</head>
<body class="fullbleed">

<template id="bind" is="dom-bind">
  <script>
    bind.changeName = function() {
      var repeater = document.querySelector('#repeater');
      //repeater.items[0].name = 'New Name' //doesn't work
      repeater.set('items.0.name', 'Another New Name'); //<==== Now it works ===

    }
  </script>
  <paper-button on-click="changeName">Change name</paper-button>

  <template id="repeater" is="dom-repeat" items='[{"name": "Foo"}, {"name": "Bar"}]'>
    <p>{{item.name}}</p>
  </template>
</template>


</body>
</html>
&#13;
&#13;
&#13;