2016-05-15 181 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中的內容)工作。 –