2016-09-17 37 views
2

我包括使用<style include="style-name"></style>的樣式,它通常工作正常。聚合物:加載動態內容時的重新加載樣式

但是,我有一個元素,它通過JavaScript動態加載和更改其內容。它會在某些時候從外部來源加載新內容並相應地更新其內容。這些新內容沒有應用於它們的style-name的樣式。

如何獲得Polymer重新評估樣式,並將其應用於新內容?


我正在使用Meteor + Synthesis如果這有所作爲。

+0

你嘗試調用'Polymer.updateStyles()'或'this.updateStyles()'? 查看[這裏](http://stackoverflow.com/questions/30854101/change-style-programmatically)瞭解更多信息 –

回答

1

爲了保持風格的範圍,你應該使用聚合物importHref()和和DOM()函數是這樣的:

// ... 
let importSource = Polymer.Base.importHref('path/to/external-source', 
// Import is done, use the body of that 
event => { 
    // #container is my element which should have the content 
    // but you can change to any element what you want 
    // or use just the this.root 
    Polymer.dom(this.$.container).innerHTML = importSource.import.body; 
}, 
// Handle errors 
event => console.error(event)); 
// ... 

這樣,如果你使用的黑幕DOM聚合物將應用該樣式範圍,在影子DOM它應該在默認情況下工作,但我沒有測試,所以更好地使用內置的功能聚合物。

Polymer.updateStyles()只有工作,當你改變本地DOM CSS變量是這樣的:this.customStyle['--my-toolbar-color'] = 'blue';

+0

重要的部分是使用'Polymer.dom(element).innerHTML =「...」;而不僅僅是'element.innerHTML'。謝謝:) – Yorrd

+0

@Yorrd Im導入的模板文件什麼是包含自定義元素,這就是爲什麼我使用importHref :)不客氣! – adaliszk