[英]Cannot read property 'toLowerCase' of undefined in Polymer
我從<input is="iron-input" bind-value="{{myValue}}">
根據為{{myValue}}
輸入的字符串,進行操作。 但是,如果用戶鍵入大寫字符串,那么他們將得到錯誤的操作。
if (this.myValue === 'abc') {
alert('lowercase str');
}
在上面的代碼之前,我有這個,但是得到了“無法讀取屬性等”:
var str = this.myValue;
var res = str.toLowerCase();
if (res === 'abc') {
alert('lowercase str');
}
有正確的方法嗎? 謝謝
編輯:
<dom-module id='my-app'>
<template>
<input is="iron-input" bind-value="{{myValue}}">
</template>
<script>
Polymer({
is: 'my-app',
properties: {
myValue: {
type: String,
observer: "selectionChanged"
}
},
selectionChanged: function () {
var str = this.myValue;
var res = str.toLowerCase();
if (res === 'abc') {
alert('lowercase str');
}
}
});
</script>
</dom-module>
確保在您的聚合物元素上定義了myValue屬性。 該代碼對我有用:
<link rel="import" href="../components/polymer/polymer.html">
<link rel="import" href="../components/iron-input/iron-input.html">
<dom-module id="x-sample">
<template>
<input is="iron-input" bind-value="{{myValue}}">
<button on-click="updateValue">myValue.toLowerCase()</button>
</template>
<script>
Polymer({
is: 'x-sample',
properties: {
myValue: String
},
updateValue: function (e) {
var str = this.myValue;
var res = str.toLowerCase();
alert(res);
}
});
</script>
</dom-module>
更新
您需要在“屬性”對象而不是根對象中定義屬性(myValue)。
我無法使用問題中發布的代碼來復制問題。 根據開發人員的回答,應將myValue對象作為屬性包括在內。 有了該修復程序,就不需要按鈕,一切似乎都可以正常工作。
<dom-module id="my-app">
<template>
<input is="iron-input" bind-value="{{myValue}}">
</template>
<script>
Polymer({
is: 'my-app',
properties: {
myValue: {
type: String,
observer: "selectionChanged"
}
},
selectionChanged: function() {
var str = this.myValue;
var res = str.toLowerCase();
if (res === 'abc') {
alert('lowercase str');
}
}
});
</script>
編輯:
另外,您可以添加一個空字符串作為默認值。
myValue: {
type: String,
observer: "selectionChanged",
value: ""
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.