Web组件:attributeChangedCallback未被触发

时间:2016-04-27 10:54:56

标签: javascript web-component

这是一个基本的hello world web组件。当属性“who”发生更改时,不会触发attributeChangedCallback。检查小提琴

MyElementProto.attributeChangedCallback = function(attr, oldVal, newVal) {
    console.log('attributeChangedCallback triggered');
        if (attr === 'who') {
            this.setWho(newVal);
        }
    };

https://jsfiddle.net/y3kj81nz/5/

1 个答案:

答案 0 :(得分:4)

您需要使用setAttribute方法:

window.changeWhoAttr = function() {
    var el = document.querySelector('#custom-tag');
    el.setAttribute('who', 'Universe'); // Instead of el.who = 'Universe'
    console.log('changeWhoAttr triggered');
}

Fiddle