从函数中注入 HTML
Inject HTML from function
我在 'Quick tour of Polymer' 后面有一节解释了如何基于数组重复元素,但它只向我们展示了如何使用模板重复器来完成,我真的不知道从后面知道它是如何工作的。我尝试做我自己的转发器,但 Polymer 将我的代码作为字符串注入,就像非转义字符一样。
代码:
<dom-module id="employee-list">
<template>
[[employe()]]
</template>
<script>
class EmployeeList extends Polymer.Element {
static get is () {
return 'employee-list'
}
constructor () {
super()
this.employees = [
{first: 'Bob', last: 'Li'},
{first: 'Ayesha', last: 'Johnson'},
{first: 'Fatma', last: 'Kumari'},
{first: 'Tony', last: 'Morelli'}
]
}
employe(employees = this.employees) {
let template = '<div>Employee List</div>'
template += employees.map((currentEmployee, id) => {
return `<div>Employee ${id}, FullName : ${currentEmployee.first + ' ' + currentEmployee.last}</div>`
})
return template
}
}
customElements.define(EmployeeList.is,EmployeeList)
</script>
</dom-module>
结果:
<div>Employee List</div><div>Employee 0, FullName : Bob Li</div>,<div>Employee 1, FullName : Ayesha Johnson</div>,<div>Employee 2, FullName : Fatma Kumari</div>,<div>Employee 3, FullName : Tony Morelli</div>
而且我想知道它是否是一种在 Polymer@2
中注入非转义字符的形式/html
您可以在函数中使用 querySelector
来实现这一点
html
<template>
<div id="employee-list"></div>
</template>
js
this.querySelector("#employee-list").innerHTML = template
正如 Jordan 所说,你应该使用 dom-repeat
<div> Employee list: </div>
<template is="dom-repeat" items="{{employees}}">
<div>First name: <span>{{item.first}}</span></div>
<div>Last name: <span>{{item.last}}</span></div>
</template>
如果您按照获取 ID 的方式进行操作,则可以使用 dom-repeat
作为替代方法。您可以使用属性 index-as
来做到这一点。
<template is="dom-repeat" items="{{employees}}" index-as="id">
您可以在此处找到有关 dom-repeat
的更多信息:https://www.polymer-project.org/1.0/docs/api/dom-repeat
我在 'Quick tour of Polymer' 后面有一节解释了如何基于数组重复元素,但它只向我们展示了如何使用模板重复器来完成,我真的不知道从后面知道它是如何工作的。我尝试做我自己的转发器,但 Polymer 将我的代码作为字符串注入,就像非转义字符一样。
代码:
<dom-module id="employee-list">
<template>
[[employe()]]
</template>
<script>
class EmployeeList extends Polymer.Element {
static get is () {
return 'employee-list'
}
constructor () {
super()
this.employees = [
{first: 'Bob', last: 'Li'},
{first: 'Ayesha', last: 'Johnson'},
{first: 'Fatma', last: 'Kumari'},
{first: 'Tony', last: 'Morelli'}
]
}
employe(employees = this.employees) {
let template = '<div>Employee List</div>'
template += employees.map((currentEmployee, id) => {
return `<div>Employee ${id}, FullName : ${currentEmployee.first + ' ' + currentEmployee.last}</div>`
})
return template
}
}
customElements.define(EmployeeList.is,EmployeeList)
</script>
</dom-module>
结果:
<div>Employee List</div><div>Employee 0, FullName : Bob Li</div>,<div>Employee 1, FullName : Ayesha Johnson</div>,<div>Employee 2, FullName : Fatma Kumari</div>,<div>Employee 3, FullName : Tony Morelli</div>
而且我想知道它是否是一种在 Polymer@2
中注入非转义字符的形式/html您可以在函数中使用 querySelector
来实现这一点
html
<template>
<div id="employee-list"></div>
</template>
js
this.querySelector("#employee-list").innerHTML = template
正如 Jordan 所说,你应该使用 dom-repeat
<div> Employee list: </div>
<template is="dom-repeat" items="{{employees}}">
<div>First name: <span>{{item.first}}</span></div>
<div>Last name: <span>{{item.last}}</span></div>
</template>
如果您按照获取 ID 的方式进行操作,则可以使用 dom-repeat
作为替代方法。您可以使用属性 index-as
来做到这一点。
<template is="dom-repeat" items="{{employees}}" index-as="id">
您可以在此处找到有关 dom-repeat
的更多信息:https://www.polymer-project.org/1.0/docs/api/dom-repeat