在Polymer 2.x中使用<iron-selector>

时间:2017-09-11 03:01:27

标签: polymer polymer-2.x iron-elements

我正在尝试演示<iron-selector>。在演示中,我想在用户从列表中选择新值时登录到控制台。我做错了什么?

Here is the JSBin.

http://jsbin.com/ciceguqore/1/edit?html,console,output
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>

  <base href="//polygit.org/polymer+:master/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer-element.html">
  <link rel="import" href="iron-selector/iron-selector.html">

</head>
<body>
  <dom-module id="my-el">
    <template>
      <iron-selector selected="[[route]]">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
      </iron-selector>
    </template>
    <script>
      class MyEl extends Polymer.Element {
        static get is() { return 'my-el' }

        static get properties() { return {
          route: {
            type: Number,
            notify: true,
            observer: '_routeChanged',
          },
        }}

        constructor() {
          super();
        }

        _routeChanged(route) {
          console.log('route', route);
        }

      }

      customElements.define(MyEl.is, MyEl);
    </script>
  </dom-module>

  <my-el></my-el>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

要更改route的值,您必须使用双向绑定。由于iron-selector无法更改route的值,因此未触发观察者。

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>

  <base href="//polygit.org/polymer+:master/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer-element.html">
  <link rel="import" href="iron-selector/iron-selector.html">

</head>

<body>
  <dom-module id="my-el">
    <template>
    <style>
    .iron-selected{color:blue}
    </style>
    <p>Route value using two way binding in iron-selector: [[route]] </p>
      <iron-selector selected="{{route}}">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
      </iron-selector>
      
      <p>Route value using one way binding in iron-selector: [[route]]</p>
      <iron-selector selected="[[route]]">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
      </iron-selector>
      [iron-selector is working but it cannot update the value of route]
    </template>
    <script>
      class MyEl extends Polymer.Element {
        static get is() {
          return 'my-el'
        }

        static get properties() {
          return {
            route: {
              type: Number,
              notify: true,
              observer: '_routeChanged',
            },
          }
        }

        constructor() {
          super();
        }

        _routeChanged(route) {
          console.log('route', route);
        }

      }

      customElements.define(MyEl.is, MyEl);
    </script>
  </dom-module>

  <my-el></my-el>
</body>

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

您还可以使用selected-changed iron-selector事件,该事件在所选属性更改时触发。 示例:

<iron-selector on-selected-changed="_onSelectedChanged" selected="[[route]]">
 <div>Item 1</div>
 <div>Item 2</div>
 <div>Item 3</div>
</iron-selector>

_onSelectedChanged(e){
  console.log('route',e.detail.value);
}