自定义函数中的聚合物未定义 属性
Polymer undefined property in custom function
我使用 Polymer (v1.0) 我想创建一个简单的元素,它可以显示一些额外的内容 取决于 属性 并且它必须使用 多个值 - a.k.a 简单的“switch”.
这段代码是我写的。
...
<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>
我的函数checkFor
用于检查我想要的值(例如'alfa')是否等于元素property/attribute for
.
但它根本不起作用,所以我尝试使用 console.log() 检查值。之后我发现变量 aaa
包含 'alfa' 并且变量 this.for
包含 undefined
。
然后我复制了一些我发现使用元素属性的输出代码,但它也没有用。如果我 运行 attached
函数中的相同代码它工作正常。
如何在自定义函数中使用元素属性?
第一次调用您的 checkFor
函数时,this.for
尚未定义。您可以将 for
作为参数传递给函数,如 documentation 中所述。
<template is="dom-if" if="{{checkFor('alfa', for)}}">
我使用 Polymer (v1.0) 我想创建一个简单的元素,它可以显示一些额外的内容 取决于 属性 并且它必须使用 多个值 - a.k.a 简单的“switch”.
这段代码是我写的。
...
<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>
我的函数checkFor
用于检查我想要的值(例如'alfa')是否等于元素property/attribute for
.
但它根本不起作用,所以我尝试使用 console.log() 检查值。之后我发现变量 aaa
包含 'alfa' 并且变量 this.for
包含 undefined
。
然后我复制了一些我发现使用元素属性的输出代码,但它也没有用。如果我 运行 attached
函数中的相同代码它工作正常。
如何在自定义函数中使用元素属性?
第一次调用您的 checkFor
函数时,this.for
尚未定义。您可以将 for
作为参数传递给函数,如 documentation 中所述。
<template is="dom-if" if="{{checkFor('alfa', for)}}">