有什么方法可以访问HTML元素中的函数吗?
Polymer的数据绑定系统设计用于将值绑定到模板。这些值通常只是文字(例如字符串和数字)或普通ole javascript对象,例如。
{a: 'someval', b: 5}
聚合物的数据绑定系统是
不
设计用于将函数绑定到模板,您不能仅在模板内使用javascript。
(If you're really into that idea, check out React as a replacement to polymer)
.
computed property
。创建一个对其他变量的更改做出反应的计算属性,而不是在模板内调用函数。当特性的状态更改时,计算的特性也将更改。这种状态可以被视为函数的参数。
<link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
<link rel="import" href="custom-js-import.html">
<dom-module id="custom-element">
<template>
<div>
Hello
</div>
<label>
<input type="number" value="{{flag::input}}">
</label>
<h1>from flag: [[flag]]</h1>
<div id="data"></div>
<div>{{boundComputedData}}</div><!-- Unable to access this from here -->
<div>{{getLocalData()}}</div>
</template>
<script>
// Define the class for a new element called custom-element
class CustomElement extends Polymer.Element {
static get is() {
return "custom-element";
}
constructor() {
super();
}
getData(flag) {
const flagAsNumber = parseInt(flag);
if (flagAsNumber === 1) {
return "one";
} else {
return "not one";
}
}
ready() {
super.ready();
this.$.data.textContent = "I'm a custom-element.";
console.log(this.getData(1)); //can be easily accessed from here
}
getLocalData() {
return "local";
}
static get properties() {
return {
flag: {
type: Number,
value: 0
},
boundComputedData: {
type: String,
computed: 'getData(flag)'
}
};
}
}
// Register the new element with the browser
customElements.define(CustomElement.is, CustomElement);
</script>
</dom-module>
<custom-element></custom-element>
所以我在这里做的是:
boundComputedData
并设置
computed
属性到
'getData(flag)'
getData
.
现在只要州政府
flag
更改时,计算属性将更新。