javascript - 如何独立使用 Polymer TemplateBinding 库?

标签 javascript html polymer template-engine

Polymer’s TemplateBinding library extends the capabilities of the HTML Template Element by enabling it to create, manage, and remove instances of content bound to data defined in JavaScript. Although internal in Polymer, it is also useful standalone.

TemplateBinding是独立的库,不依赖于 Polymer。因此理论上可以在没有 polymer 的情况下使用它。

我找不到任何如何使用这个独立的示例。

例如,我有这样的标记

<ul>
    <template id="colors" repeat="{{ colors }}">
      <li style="color: {{ color }}">The style attribute of this list item is bound</li>
    </template>
</ul>

和 json

colors: [
          { color: 'red' },
          { color: 'blue' },
          { color: 'green' },
          { color: 'pink' }
        ]

我正在寻找类似函数的东西,它接受模板和数据并返回处理后的标记。

最佳答案

独立是什么意思?您的意思是在 Polymer Web 组件之外使用 Polymer?

如果是这样,您可以使用自动绑定(bind)属性,例如

<template id="colors" repeat="{{ colors }}" is="auto-binding">

这将允许 Polymer 在 Light-DOM 中看到您的模板(页面上的任何位置)

这是一个工作 fiddle :http://jsfiddle.net/Lm7tgbLo/

HTML 代码:

<script src="http://www.polymer-project.org/polymer.min.js"></script>

<ul>
    <template id="colors" repeat="{{color in colors}}" is="auto-binding">
      <li style="color: {{ color }}">The style attribute of this list item is bound</li>
    </template>
</ul>

JavaScript:

window.addEventListener('polymer-ready', function (e) {

    document.getElementById("colors").colors = ['red', 'blue', 'green', 'pink'];

});

关于javascript - 如何独立使用 Polymer TemplateBinding 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28861999/

相关文章:

javascript - Ti.Storekit 钛

javascript代码不允许页面在未登录的情况下加载

html - 页脚栏格式不正确

html - 仅针对文本将文本与背景色居中对齐

javascript - polymer 重复模板双向数据绑定(bind)

javascript - 如何使用 Polymer dom-repeat 映射数组

javascript - 清除 jquerymobile 应用程序的后台堆栈

html - 正斜杠在 html img 标签中做什么

javascript - polymer 未捕获的 DOMException

javascript - rails : Javascript for tracking sessions firing inconsistently