简体   繁体   English

处理Ember.js中的动作参数

[英]Handling action parameter in Ember.js

In template: 在模板中:

<button {{action someAction someParameter}}>Some Action</button>

In controller: 在控制器中:

someAction: function (e, someParameter) {
    console.log(e, someParameter);
}

someParameter is undefined as well as e where I excepted to be event object. someParameter是未定义的,以及我除了作为事件对象的e

How to pass parameter to action? 如何将参数传递给动作? If not possible does it mean I need to create Ember.View to handle action with parameter? 如果不可能,这是否意味着我需要创建Ember.View来处理带参数的动作?

The only problem I see in your code is that the event object is not passed to the function in the controller when using the {{action}} helper . 我在代码中看到的唯一问题是, 在使用{{action}}帮助程序时, 事件对象不会传递给控制器​​中的函数 Regardless of that, your code should log the value of someParameter to the console. 无论如何,您的代码应该将someParameter的值someParameter到控制台。 If you are getting two undefined maybe someParameter is not within the context of the template , or it is undefined . 如果你得到两个undefined可能someParameter 不在模板的上下文中 ,或者它是undefined

Make sure someParameter is there and holds the correct value, for example: 确保someParameter在那里并保持正确的值,例如:

Template: 模板:

<button {{action someAction someParameter}}>Some Action (param: {{someParameter}} )  </button>

If the value doesn't show up, try view.someParameter depending on how you are rendering the template, if you show your code we might be able to help you more. 如果该值未显示,请尝试view.someParameter具体取决于您呈现模板的方式,如果您显示代码,我们可能会为您提供更多帮助。

On the controller: 在控制器上:

someAction: function (someParameter) {
  console.log(someParameter);
}

Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM