admin管理员组文章数量:1355825
In version 0.5 it was easy:
<polymer-element name="textarea-tpl" attributes="value placeholder">
<template>
<link rel="stylesheet" type="text/css" href="css/index.css">
<textarea id="textarea" value="{{value}}" placeholder="{{placeholder}}"></textarea>
<textarea id="hidden_textarea"></textarea>
</template>
<script>
Polymer({
ready: function() {
var text = this.$.textarea;
var hidden_text = this.$.hidden_textarea;
text.onkeyup = function() {
hidden_text.value = text.value + "\n";
var height = hidden_text.scrollHeight;
text.style.height = height+'px';
};
}
});
</script>
</polymer-element>
In version 1.0 this binding doesn't work. Only write works and which is strange, only one time. Code for v1.0:
<dom-module id="chat-textarea">
<template>
<textarea id="textarea" value="{{value}}" placeholder="{{placeholder}}"></textarea>
<textarea id="hidden_textarea"></textarea>
</template>
<script>
Polymer({
is: "chat-textarea",
properties: {
value: String,
placeholder: String
},
set text(val) {
this.$.textarea.value = val;
},
get text() {
return this.$.textarea.value;
},
ready: function() {
var text = this.$.textarea;
var hidden_text = this.$.hidden_textarea;
text.onkeyup = function() {
hidden_text.value = text.value + "\n";
var height = hidden_text.scrollHeight;
text.style.height = height+'px';
};
}
});
</script>
</dom-module>
Now I use set\get text, but it's not property and available only from JS.
In iron-autogrow-textarea is written: Because the textarea's value property is not observable, you should use this element's bind-value instead for imperative updates. But why in 0.5 textarea's value was observable?
In version 0.5 it was easy:
<polymer-element name="textarea-tpl" attributes="value placeholder">
<template>
<link rel="stylesheet" type="text/css" href="css/index.css">
<textarea id="textarea" value="{{value}}" placeholder="{{placeholder}}"></textarea>
<textarea id="hidden_textarea"></textarea>
</template>
<script>
Polymer({
ready: function() {
var text = this.$.textarea;
var hidden_text = this.$.hidden_textarea;
text.onkeyup = function() {
hidden_text.value = text.value + "\n";
var height = hidden_text.scrollHeight;
text.style.height = height+'px';
};
}
});
</script>
</polymer-element>
In version 1.0 this binding doesn't work. Only write works and which is strange, only one time. Code for v1.0:
<dom-module id="chat-textarea">
<template>
<textarea id="textarea" value="{{value}}" placeholder="{{placeholder}}"></textarea>
<textarea id="hidden_textarea"></textarea>
</template>
<script>
Polymer({
is: "chat-textarea",
properties: {
value: String,
placeholder: String
},
set text(val) {
this.$.textarea.value = val;
},
get text() {
return this.$.textarea.value;
},
ready: function() {
var text = this.$.textarea;
var hidden_text = this.$.hidden_textarea;
text.onkeyup = function() {
hidden_text.value = text.value + "\n";
var height = hidden_text.scrollHeight;
text.style.height = height+'px';
};
}
});
</script>
</dom-module>
Now I use set\get text, but it's not property and available only from JS.
In iron-autogrow-textarea is written: Because the textarea's value property is not observable, you should use this element's bind-value instead for imperative updates. But why in 0.5 textarea's value was observable?
Share Improve this question edited Jul 23, 2015 at 19:10 Denis535 asked Jul 23, 2015 at 18:26 Denis535Denis535 3,6004 gold badges28 silver badges38 bronze badges1 Answer
Reset to default 9To bind to an inputted value in Polymer 1.0 add ::input after the variable you're binding to.
Example:
<textarea value="{{taValue::input}}"></textarea>
Here is a working example on plnkr
Elements like iron-input use the bind-input attribute to automatically bind the variable.
More info is in the docs for two-way binding
本文标签: javascriptPolymerjs twoway binding to textarea valueStack Overflow
版权声明:本文标题:javascript - Polymer.js two-way binding to textarea value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744054393a2583006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论