Polymer 聚合物属性变化函数
我有一个用货币和价格属性定义的元素。当我将货币属性设置为英镑时,我想相应地计算价格。我尝试挂接attrChanged方法,但if条件从未记录为truePolymer 聚合物属性变化函数,polymer,web-component,Polymer,Web Component,我有一个用货币和价格属性定义的元素。当我将货币属性设置为英镑时,我想相应地计算价格。我尝试挂接attrChanged方法,但if条件从未记录为true currencyChanged: function (attrName, oldVal, newVal) { var actualPrice = this.getAttribute("usdprice"); actualPrice = parseInt(actualPrice); var converter = 1;
currencyChanged: function (attrName, oldVal, newVal) {
var actualPrice = this.getAttribute("usdprice");
actualPrice = parseInt(actualPrice);
var converter = 1;
if(newVal === "£") {
converter = 0.59;
console.log("true");
}
this.$.price.innerHTML = actualPrice*converter;
}
我使用下面的命令更改货币
var a = document.querySelector("gmselect-element");
a.setAttribute("currency", "£");
使用likecurrencyChanged
时,参数为(oldVal,newVal)。只有在创建泛型时才能获得(attrName、oldVal、newVal)。试试这个:
currencyChanged: function (oldVal, newVal) {
var actualPrice = this.getAttribute("usdprice");
actualPrice = parseInt(actualPrice);
var converter = 1;
if(newVal === "£") {
converter = 0.59;
console.log("true");
}
this.$.price.innerHTML = actualPrice*converter;
}
打开上的Dev Tools控制台,查看它是否正常工作