聚合物:在数据绑定表达式中使用函数 [英] polymer: using a function in a data binding expression

查看:79
本文介绍了聚合物:在数据绑定表达式中使用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Polymer元素的属性之一是时间戳:

One of the properties of my Polymer element is a timestamp:

Polymer({
  timestamp: null,
  ready: function () {
    this.timestamp = new Date().getTime();
  }
});

在我的标记中,我需要显示一个格式化的时间戳记:

In my markup I need to show a formatted timestamp:

<template>
  timestamp={{getFormattedTimestamp()}}
</template>

其中

getFormattedTimestamp: function () {
  return new Date(this.timestamp).toLocaleString('en-US', {timeZoneName: 'short'});
}

但是,当this.timestamp更改时,上面的代码不会更新显示,这是因为Polymer不知道发生这种情况时需要调用getFormattedTimestamp.

But the code above does not update the display when this.timestamp changes because Polymer does not know that it needs to call getFormattedTimestamp when this happens.

我不能只将getFormattedTimestamp的内容放入数据绑定表达式中,因为它无法解析它,并且出于相同的原因,我也无法创建计算属性.

I cannot just put the contents of getFormattedTimestamp into the data binding expression because it won't be able to parse it, and I cannot create a computed property either for the same reason.

除了创建一个保存格式化时间戳记并在时间戳记更改时对其进行更新的辅助成员变量之外,还有其他方法吗?

Is there a way to do it other than creating an auxiliary member variable that holds the formatted timestamp and updating it when the timestamp changes?

推荐答案

您是否查看过过滤器表达式?

您可以有类似这样的内容:

You can have something like this:

<template>
  timestamp={{timestamp | getFormattedTimestamp }}
</template>

这篇关于聚合物:在数据绑定表达式中使用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆