Polymer 如何使用dom绑定观察属性更改
如果使用Polymer 如何使用dom绑定观察属性更改,polymer,polymer-1.0,Polymer,Polymer 1.0,如果使用dombind,如何观察属性更改?属性中的更改在花括号内更新,所以我假设有一些与更改相关的事件,但我不知道它的名称 例如: <!doctype html> <html lang=""> <head > <link rel="import" href="bower_components/polymer/polymer.html"> <script src="bower_components/webcomponentsjs/web
dombind
,如何观察属性更改?属性中的更改在花括号内更新,所以我假设有一些与更改相关的事件,但我不知道它的名称
例如:
<!doctype html>
<html lang="">
<head >
<link rel="import" href="bower_components/polymer/polymer.html">
<script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<style>
</style>
</head>
<body>
<template is="dom-bind" id="app2">
<p>test: <span>{{test}}</span></p>
<input type="button" value="Change" on-click="chTest">
</template>
<script>
(function (document) {
'use strict';
var app = document.querySelector('#app2');
app.test = "original value";
app.addEventListener('test-changed', function () {
console.log('change listener fired');
});
app.addEventListener('dom-change', function () {
console.log("dom-change");
});
app.chTest = function () {
console.log('click');
app.test = "new value";
// notifyPath-setter will fire "test-changed"-event
// app.notifyPath("test", "notify value");
};
})(document);
</script>
</body>
</html>
测试:{{test}
(职能(文件){
"严格使用",;
var app=document.querySelector('#app2');
app.test=“原始值”;
app.addEventListener('test-changed',函数(){
log('change listener fired');
});
app.addEventListener('dom-change',函数(){
log(“dom更改”);
});
app.chTest=函数(){
console.log('click');
app.test=“新值”;
//notifyPath setter将触发“测试已更改”-事件
//应用程序notifyPath(“测试”、“通知值”);
};
})(文件);
编辑:
有必要澄清我的问题:当app.test发生变化时,我想调用一些函数。你已经把答案注释掉了。注释行app.test并取消注释app.notifyPath
// app.test = "new value";
// notifyPath-setter will fire "test-changed"-event
app.notifyPath("test", "notify value");
工作地点如下:
编辑:一种解决方法是将其双向绑定到子元素中并侦听其更改。我已经更新了jsbin。你已经把答案注释掉了。注释行app.test并取消注释app.notifyPath
// app.test = "new value";
// notifyPath-setter will fire "test-changed"-event
app.notifyPath("test", "notify value");
工作地点如下:
编辑:一种解决方法是将其双向绑定到子元素中并侦听其更改。我已经更新了jsbin。很抱歉,我没有充分解释我的示例。NotifyPath在代码中仅用于测试事件侦听器,即。如果使用NotifyPath,则测试更改的侦听器将响应。我的问题是如何侦听app.test-property中的更改。我想在app.test发生变化时调用一些函数,明白了。一种解决方法是将值双向绑定到子元素并侦听其更改。我已经更新了jsbin。这似乎是目前最简单的解决方案。基本上,您需要将一个简单的自定义元素(例如:)添加到dom绑定模板中。观察到的属性绑定到自定义元素,每次属性更改时,您的更改函数都会触发。很抱歉,我没有充分解释我的示例。NotifyPath在代码中仅用于测试事件侦听器,即。如果使用NotifyPath,则测试更改的侦听器将响应。我的问题是如何侦听app.test-property中的更改。我想在app.test发生变化时调用一些函数,明白了。一种解决方法是将值双向绑定到子元素并侦听其更改。我已经更新了jsbin。这似乎是目前最简单的解决方案。基本上,您需要将一个简单的自定义元素(例如:)添加到dom绑定模板中。观察到的属性绑定到自定义元素,每次更改属性时都会触发change函数。