Databinding of polymer paper_input does not work

2020-04-15 23:55发布

I have the following code where a 2-way binding is expected between first_name and the property val.

  <!DOCTYPE html>

  <link rel="import" href="packages/polymer/polymer.html">

  <link rel="import" href="packages/paper_elements/paper_input.html" >

  <polymer-element name='paper-input-snippet'>
    <template>

    <div on-mouseout="{{zoom}}">
      <paper-input
        label="First" floatinglabel
        error="Invalid input"

        id="first_name"
        value='{{val}}'>
      </paper-input>

      <paper-input
        label="Middle" floatinglabel
        error="Invalid input"

        id="middle_name">
      </paper-input>

      <paper-input
        label="Last" floatinglabel
        error="Invalid input"

        id="last_name">
      </paper-input>

    </div>

    </template>

    <script type="application/dart">

    import 'package:polymer/polymer.dart';
    import 'dart:html';

    import 'package:paper_elements/paper_input.dart';

    @CustomTag( 'paper-input-snippet' )
    class PaperInputSnippet extends PolymerElement
    {
      @observable String val = 'Tommy';
      String receiver = '';

      PaperInputSnippet.created() : super.created();

      void zoom( Event e, var detail )
      {
        print ( e.target );
        print ( val);
      }

      @override
      void attached()
      {
         super.attached();
         receiver = this.dataset['receiver'];
      }
    }

    </script>
  </polymer-element>

However, changing the value of the paper-element does not print the new value, only the intialized value is printed.

Is this a bug or am I doing something wrong>

1条回答
家丑人穷心不美
2楼-- · 2020-04-16 00:46

EDIT

An updated Polymer packages was just published that should fix this issue see https://groups.google.com/a/dartlang.org/forum/#!topic/bugs/-2f-TeaOoac for more details.

You need to adjust your dependency constraint to get this update because its a development release

polymer: ">= 0.11.1-dev < 0.12.0" 

EDIT END

There is an open issue for this code.google.com/p/dart/issues/detail?id=19794

Until it is fixed, you can work around it by adding an on-change event handler:

<paper-input
    label="First" floatinglabel
    error="Invalid input"

    id="first_name"
    value='{{val}}'
    on-change='{{firstNameChangeHandler}}'
    >
  </paper-input>
  void firstNameChangeHandler() {
    val = ($['first_name'] as PaperInput).value;
  }
查看更多
登录 后发表回答