如果我有以下dom-repeat模板:
<template is="dom-repeat" items="{{myFiles}}" as="file">
<span>
{{file.createDate}} <br/>
</span>
</template>我想格式化file.createDate,有什么方法可以用计算性质来完成吗?
发布于 2016-11-29 15:43:07
不,您需要在项上使用计算绑定 (或者在本例中是它的子属性):
// template
<template is="dom-repeat" items="{{_myFiles}}" as="file">
<span>{{_formatDate(file.createDate)}}</span>
</template>
// script
Polymer({
_formatDate: function(createDate) {
return /* format createDate */;
}
});或者,您可以在myFiles数组上使用计算属性(例如,名为myFiles),该属性将在dom-repeat迭代之前处理所有项:
// template
<template is="dom-repeat" items="{{_myFiles}}" as="file">
<span>[[file.createDate]]</span>
</template>
// script
Polymer({
properties: {
myFiles: Array,
_myFiles: {
computed: '_preprocessFiles(myFiles)'
}
},
_preprocessFiles: function(files) {
return files.map(x => {
x.createDate = /* format x.createDate */;
return x;
});
}
});https://stackoverflow.com/questions/40869721
复制相似问题