如何更新 Polymer 1.0 中的 textarea 和 iron-autogrow-textarea 文本值?

How to update textarea and iron-autogrow-textarea text values in Polymer 1.0?

我在 Polymer 中设置了与正常 textarea 的双向绑定,使用:

<textarea id="textbox" value="{{editText::input}}" autofocus></textarea> 

我还尝试了使用 bindValue 属性的双向绑定 iron-autogrow-textarea

<iron-autogrow-textarea bindValue="{{editText}}" class="fit" autofocus></iron-autogrow-textarea>

属性editText赋值如下:

Polymer({
  is: "page-editor",
  properties: {
    editText: {
      type: String,
      value: ""
    }
  },

但是当更改下面代码中的 editText 时,它不会更新相应的文本区域值...

this.editText = "new message";

有趣的是 console.log(this.editText) 说它 'undefined'

我仍在加紧使用 Polymer,但我认为您需要将 notify 设置为 true

Polymer({
  is: "page-editor",
  properties: {
    editText: {
      type: String,
      value: "",
+     notify: true
    }
  },
...

如果这不起作用,post 一个完整的示例,我很乐意与您一起调试。

您可以使用 Polymer 的 on-* 语法添加事件侦听器,其中 * 是要侦听的事件。

<iron-autogrow-textarea bindValue="{{editText}}" 
                        class="fit" 
                        on-click="f' 
                        autofocus></iron-autogrow-textarea>

定义事件侦听器:

Polymer({
  is: "page-editor",
  properties: {
    editText: {
      type: String,
      value: "",
      notify: true
    }
  },
  /* the function signature below may be wrong...
   * don't know how many arguments it takes... */
  f: function(e, detail, sender) {
    this.editText = 'yay';
  }
...

要使用的正确属性是 bind-value="{{editText}}"。 CamelCase 属性转换为带破折号的属性 (source)。