2016-05-15 182 views
0

在index.html的使用:聚合物1.4 DOM绑定定义properrties

<template is="dom-bind" id="app"> 

在一个正常的DOM的模块使用:

<script> 
    (function() { 
     'use strict'; 
     Polymer({ 
     is: 'my-xxxx', 
     properties: { 
     /* location for properties */ 
     } 

我在哪里确定在使用的属性模板dom绑定(fi有一个观察者连接到他们?

回答

0

基于聚合物docs,你会使用JavaScript(内联或导入index.html)添加模板属性insid e为模板的dom-change事件的事件处理程序。例如,你可以一个message属性添加到模板,这个脚本:

var t = document.getElementById('app'); 
t.addEventListener('dom-change', function() { 
    t.message = 'Hello world!'; 
}); 
下面

观看演示:

<head> 
 
    <base href="https://polygit.org/polymer+:master/components/"> 
 
    <script src="webcomponentsjs/webcomponents-lite.min.js"></script> 
 
    <link rel="import" href="polymer/polymer.html"> 
 
</head> 
 

 
<body> 
 
    <template is="dom-bind" id="app"> 
 
    {{message}} 
 
    </template> 
 

 
    <script> 
 
    var t = document.getElementById('app'); 
 

 
    // The dom-change event signifies when the template has stamped its DOM. 
 
    t.addEventListener('dom-change', function() { 
 
     // auto-binding template is ready. 
 
     t.message = 'Hello world!'; 
 
    }); 
 
    </script> 
 
</body>

jsbin

+0

这适用于加入属性的初始值。但是,如何定义通常在聚合物属性中声明的其他元素,如类型(数字,字符串等)注释,如readOnly和大多数(如果全部观察者) –

+0

没有聚合物API从自动绑定模板观察属性(或创建属性对象) 。你为什么不直接使用聚合物元素呢? – tony19

+0

这就是我要做的,但聚合物入门套件(PSK)应用程序不会这样做,我们必须学习我们的学生根据建议(PSK中的内容)工作。 –