[英]Polymer undefined property in custom function
I use Polymer (v1.0) and I want to create a simple element which can show me some extra content depending on a property and it has to work with multiple values - aka simple " switch ". 我使用Polymer (v1.0),并且我想创建一个简单的元素,该元素可以根据属性显示一些额外的内容,并且它必须使用多个值 -也就是简单的“ switch ”。
I wrote this code. 我写了这段代码。
...
<span>{{for}}</span>
<template is="dom-if" if="{{checkFor('alfa')}}">
hi !!!
</template>
</template>
<script>
Polymer({
is: "...",
properties: {
for: String,
user: String,
manager: {
type: Boolean,
notify: true
}
},
attached: function() {
this.textContent = 'Hello World, my user is ' + (this.user || 'nobody') + '.\n' +
'This user is ' + (this.manager ? '' : 'not') + ' a manager and he likes ' + this.for + '.';
},
checkFor: function (aaa) {
// return aaa === this.for;
this.textContent = 'Hello World, my user is ' + (this.user || 'nobody') + '.\n' +
'This user is ' + (this.manager ? '' : 'not') + ' a manager and he likes ' + this.for + '.';
}
});
</script>
My function checkFor
is used to check if a value I want to have (for example 'alfa') is equal with element property/attribute for
. 我的功能checkFor
用来检查一个值我想要(例如“阿尔法”)是与元件特性/属性等于for
。
But it doesn't worked at all so I tried to check values using console.log(). 但这根本不起作用,因此我尝试使用console.log()检查值。 After than I discovered variable aaa
contains 'alfa' and variable this.for
contains undefined
. 之后,我发现变量aaa
包含'alfa',变量this.for
包含undefined
。
Then I copied some output code I found which use element properties and it also did not work. 然后,我复制了一些发现的输出代码,这些代码使用了元素属性,并且也不起作用。 If I run the same code in attached
function it works fine. 如果我在attached
函数中运行相同的代码,则可以正常工作。
How can I use element properties in custom functions? 如何在自定义函数中使用元素属性?
The first time your checkFor
function is called, this.for
has not been defined yet. 第一次调用checkFor
函数时,尚未定义this.for
。 You can pass in for
as a parameter to the function, as described in the documentation . 您可以按照文档中的说明将for
作为参数传递给函数。
<template is="dom-if" if="{{checkFor('alfa', for)}}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.