ant*_*ony 2 javascript web-component polymer
我正在编写一个使用Polymer 1.0的辅助函数加载html文件的简单元素importHref().页面加载,但不是html呈现到页面,我得到[object HTMLDocument].
当我记录成功的回调时,导入的页面被包装在一个#document对象中(这里的术语不确定).但是,信息就在控制台中.
所以,我的问题是:如何将html呈现给页面?
元件:
<dom-module id="content-loader">
<template>
<span>{{fileContent}}</span>
</template>
<script>
Polymer({
is: "content-loader",
properties: {
filePath: {
type: String
}
},
ready: function() {
this.loadFile();
},
loadFile: function() {
var baseUrl;
if (!window.location.origin)
{
baseUrl = window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
else
{
baseUrl = window.location.origin;
}
//import html document and assign to fileContent
if(this.filePath)
{
this.importHref(baseUrl + this.filePath, function(file){
this.fileContent = file.target.import;
console.log(this.fileContent); //logs fine
},
function(error){
console.log(error);
});
}
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
正在使用:
<content-loader file-path="/app/general/contact.html"></content-loader>
Run Code Online (Sandbox Code Playgroud)
<span>{{fileContent}}</span>将呈现fileContent转换成String,这就是为什么你看到[object HTMLDocument](这是你当你调用得到什么toString()上document对象).
通常,Polymer不会让您绑定到HTML或节点内容,因为它存在安全风险.
在fileContent你已经是一个document,这意味着它的DOM节点的集合.您如何使用该文档取决于您加载的内容.渲染节点的一种方法是附加fileContent.body到本地DOM,如下所示:
Polymer.dom(this.root).appendChild(this.fileContent.body);
这是一个更完整的例子(http://jsbin.com/rafaso/edit?html,output):
<content-loader file-path="polymer/bower.json"></content-loader>
<dom-module id="content-loader">
<template>
<pre id="content"></pre>
</template>
<script>
Polymer({
is: "content-loader",
properties: {
filePath: {
type: String,
observer: 'loadFile'
}
},
loadFile: function(path) {
if (this.filePath) {
console.log(this.filePath);
var link = this.importHref(this.filePath,
function() {
Polymer.dom(this.$.content).appendChild(link.import.body);
},
function(){
console.log("error");
}
);
}
}
});
</script>
</dom-module>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5951 次 |
| 最近记录: |