聚合物:如何根据属性替换标签名称?
Polymer: how to replace tag name depend on attribute?
我有这样的模板
<template>
<button class="y-button" id="el" disabled?="{{disabled !== undefined}}">
<span class="y-button__text"><content></content></span>
</button>
</template>
如何将标签名称更改为另一个名称(例如 <a>
)取决于某些属性,例如
<my-button url="http://example.org"></my-button>
?
我试过了
<template>
<{{tagName}}></{{tagName}}>
</template>
但它不起作用。
数据绑定无法做到这一点。绑定表达式只允许出现在标签的文本内容或属性值中:
https://www.polymer-project.org/docs/polymer/binding-types.html#node-bindings
您也不能使用数据绑定插入整个标签,因为数据绑定表达式的内容在插入前会被 HTML 转义。参见:
https://www.polymer-project.org/docs/polymer/expressions.html#expression-syntax
"You can’t insert HTML using expressions. To avoid XSS issues, the output of an expression is HTML escaped before being inserted as the value of the binding."
如果你只有两个可能的标签,你可以使用条件模板语法:
<template if="{{condition}}">
<tagOne></tagOne>
</template>
<template if="{{! condition}}">
<tagTwo></tagTwo>
</template>
对于更复杂的情况,您可能需要按照@skmvasu 的建议使用JavaScript 和innerHTML。如果您需要在插入的 HTML 中 inside 激活数据绑定,您可以使用 injectBoundHTML 方法:
https://www.polymer-project.org/docs/polymer/databinding-advanced.html#boundhtml
您可以使用 injectBoundHTML
:
var tag = 'x-el';
var html = '<' + tag + ' item="{{item}}"></' + tag + '>';
this.injectBoundHTML(html, this.$.container);
<script src="http://www.polymer-project.org/webcomponents.min.js"></script>
<script src="http://www.polymer-project.org/polymer.js"></script>
<polymer-element name="x-el" attributes="item" noscript>
<template>
{{item.name}}
</template>
</polymer-element>
<polymer-element name="my-element">
<template>
<div id="container"></div>
</template>
<script>
Polymer({
ready: function() {
this.item = {name: 'John Smith'};
var tag = 'x-el';
var html = '<' + tag + ' item="{{item}}"></' + tag + '>';
this.injectBoundHTML(html, this.$.container);
}
});
</script>
</polymer-element>
<my-element></my-element>
我有这样的模板
<template>
<button class="y-button" id="el" disabled?="{{disabled !== undefined}}">
<span class="y-button__text"><content></content></span>
</button>
</template>
如何将标签名称更改为另一个名称(例如 <a>
)取决于某些属性,例如
<my-button url="http://example.org"></my-button>
?
我试过了
<template>
<{{tagName}}></{{tagName}}>
</template>
但它不起作用。
数据绑定无法做到这一点。绑定表达式只允许出现在标签的文本内容或属性值中:
https://www.polymer-project.org/docs/polymer/binding-types.html#node-bindings
您也不能使用数据绑定插入整个标签,因为数据绑定表达式的内容在插入前会被 HTML 转义。参见:
https://www.polymer-project.org/docs/polymer/expressions.html#expression-syntax
"You can’t insert HTML using expressions. To avoid XSS issues, the output of an expression is HTML escaped before being inserted as the value of the binding."
如果你只有两个可能的标签,你可以使用条件模板语法:
<template if="{{condition}}">
<tagOne></tagOne>
</template>
<template if="{{! condition}}">
<tagTwo></tagTwo>
</template>
对于更复杂的情况,您可能需要按照@skmvasu 的建议使用JavaScript 和innerHTML。如果您需要在插入的 HTML 中 inside 激活数据绑定,您可以使用 injectBoundHTML 方法:
https://www.polymer-project.org/docs/polymer/databinding-advanced.html#boundhtml
您可以使用 injectBoundHTML
:
var tag = 'x-el';
var html = '<' + tag + ' item="{{item}}"></' + tag + '>';
this.injectBoundHTML(html, this.$.container);
<script src="http://www.polymer-project.org/webcomponents.min.js"></script>
<script src="http://www.polymer-project.org/polymer.js"></script>
<polymer-element name="x-el" attributes="item" noscript>
<template>
{{item.name}}
</template>
</polymer-element>
<polymer-element name="my-element">
<template>
<div id="container"></div>
</template>
<script>
Polymer({
ready: function() {
this.item = {name: 'John Smith'};
var tag = 'x-el';
var html = '<' + tag + ' item="{{item}}"></' + tag + '>';
this.injectBoundHTML(html, this.$.container);
}
});
</script>
</polymer-element>
<my-element></my-element>