use*_*612 5 javascript html5 dom onclick polymer-1.0
我正在尝试使用聚合物,但是在注册方法和调用它们时遇到了问题,我在互联网上尝试了一切,似乎没有任何工作,我在这上花了太多时间,但没有结果,请帮助我
这是我的代码:
<dom-module id="products-list">
<template>
<template is="dom-bind">
<iron-ajax id="ajax" auto
handle-as="json"
last-response="{{ajaxResponse}}"></iron-ajax>
<div class="main">
<template is="dom-repeat" items="[[ajaxResponse.dataResult]]">
<paper-card heading="[[item.name]]" image="[[item.image]]" class="pb16 w100 flex layout horizontal">
<div class="card-content">[[item.desc]]</div>
<div class="card-actions">
<paper-item>
<strong>[[item.price]]</strong>
<div class="flex"></div>
<paper-button on-click="_addToCart" raised><iron-icon icon="icons:add-shopping-cart"></iron-icon>Add to cart</paper-button>
</paper-item>
</div>
</paper-card>
</template>
</div>
</template>
</template>
<script>
Polymer({
is: "products-list",
ready: function() {
var baseUrl = getBaseURL();
var token = getAccessToken();
var namespace = getNamespace();
var appKey = getAppKey();
var appSecret = getAppSecret();
var url = baseUrl + '/stream/product.json';
var params = {
'page' : 0,
'size' : 25
};
var headers = {
'X-Auth-Token' : token,
'X-NAME-SPACE' : namespace,
'X-APP-KEY' : appKey,
'X-APP-SECRET' : appSecret,
};
var ajax = document.querySelector("#ajax");
ajax.url = url;
ajax.headers = headers;
},
properties: {
},
_addToCart: function (e) {
console.log('You pressed button ' + e.model.item.name);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
一切正常,除了单击按钮,我收到以下错误:
[dom-bind::_createEventHandler]: listener method `_addToCart` not defined
Run Code Online (Sandbox Code Playgroud)
删除包装dom-bind一切都会正常工作.
<template is="dom-bind">...</template>
Run Code Online (Sandbox Code Playgroud)
元素定义中不需要dom-bind.它旨在用于普通的HTML页面,并为您的元素启动一个新的"绑定上下文".这就是它找不到方法的原因,因为它试图在元素定义之外找到事件处理程序方法.
| 归档时间: |
|
| 查看次数: |
3909 次 |
| 最近记录: |