简体   繁体   English

在Ember中,每当属性更改时,如何在控制器中调用函数

[英]In Ember How Should I Call a Function in the Controller Whenever A Property Changes

I would like the function exampleFunction to be called every time `exampleProperty' changes. 我希望每次`exampleProperty'更改时都调用函数exampleFunction How should I set that up? 我应该如何设置? Seems this has changed recently. 似乎最近情况有所改变。 I'm using 1.0.0-RC1. 我正在使用1.0.0-RC1。

The template: 模板:

   <div id="container">
      {{view Ember.TextField valueBinding="exampleProperty"}}
    </div>

The controller: 控制器:

ExampleApp.ApplicationController = Ember.Controller.extend({

  exampleProperty: "example",

  exampleFunction: function() {
    console.log("exampleProperty was changed");
  }

});

You would define exampleFunction as an observer. 您可以将exampleFunction定义为观察者。 The most straightforward syntax is to use the 'observes' method on Function as such: 最直接的语法是在Function上使用“ observes”方法,如下所示:

exampleFunction: function () {
    console.log("property changed");
}.observes('exampleProperty')

You can read more about observers (and more) at the Ember guide: http://emberjs.com/guides/object-model/observers/ 您可以在Ember指南中阅读有关观察者的更多信息(以及更多信息): http : //emberjs.com/guides/object-model/observers/

You would create a "observes" property. 您将创建一个“观察”属性。

ExampleApp.ApplicationController = Ember.Controller.extend({

  exampleProperty: "example",

  exampleFunction: function() {
    console.log("exampleProperty was changed");
  }.observes("exampleProperty")

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Ember中刷新控制器属性? - How to refresh a controller property in Ember? 从全局函数调用Ember控制器上的方法 - Call method on an Ember controller from a global function 每当Knockout JS中可观察到的属性发生变化时,都要执行一个函数 - Execute a function whenever a property of an observable changes in Knockout JS 加载Ember JS控制器后如何启动函数? - How do I initiate a function when an Ember JS controller loads? 我应该如何为每个ajax函数调用相同的ajax路由URL,即使它位于laravel 5.5中的另一个控制器中 - How should I call same ajax route url for every ajax function even if it is in another controller in laravel 5.5 Ember:如何正确设置Controller属性 - Ember: how to set correctly Controller property 每当我触摸时,都会使函数调用onclick - Making a function call onclick whenever I touch 为什么没有对ajax调用spring控制器应该做些什么改变? - why ajax call is not made to spring Controller what changes should I do? 我想出了一种将数字格式化为科学计数法的方法,我如何在需要时调用这个 function? - I came up with a way to format numbers to scientific notation, how can I call this function whenever I need it? 如何在角度控制器中调用函数? - How can I call a function in an angular controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM