【问题标题】:How to call a function in javascript on a key如何在键上调用javascript中的函数
【发布时间】:2015-10-01 18:21:26
【问题描述】:
`import Ember from 'ember'`

 DatePickerComponent = Ember.Component.extend
 dateFormat: 'dd.mm.yy'
 today: currentDate
 currentDate: ->
   today =  Date()
   today

export default DatePickerComponent

参考错误,当前日期未定义。也得到它的时候 today: currentDate

我怎么称呼它?

我想访问今天的日期我的车把文件。

【问题讨论】:

  • today: this.currentDate
  • 我收到一个错误,说这没有在顶层定义
  • @sbatson5 那行不通。

标签: javascript ember.js coffeescript handlebars.js


【解决方案1】:

我建议返回并深入研究 Ember 对象模型和计算属性。模板可以显示的是属性。你想要的

currentDate: function() {
  return Date();
}.property()

或其等效的 CS。

然后在你的模板中

Today is {{currentDate}}.

当你说

today: currentDate

您将today 属性的值设置为名为@9​​87654325@ 的局部变量,而不是名为currentDate 的对象属性。没有名为currentDate 的局部变量。因此ReferenceError。如果你想设置一个属性等于另一个属性的值(虽然这里似乎没有必要),你可以这样做:

today: function() {
  return this.get('currentDate');
}.property('currentDate')

或者更简单

today: Ember.computed.alias('currentDate')

【讨论】:

  • 非常感谢,这正是我想要的。
猜你喜欢
  • 2016-05-02
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 2015-12-11
  • 2019-04-04
相关资源
最近更新 更多