简体   繁体   English

Ember无法将参数传递给动作

[英]Ember cannot pass parameter into action

It constantly gives me value undefined no matter what value I put in value or if I change it to onclick="{{action ...}}". 无论我输入的是什么值,还是将其更改为onclick =“ {{action ...}}}”,它都会不断为我带来不确定的价值。 I've literally tried every combination I could think of. 我已经尝试了所有我能想到的组合。 I've even used a tags to see if that will save the value. 我甚至使用标签,看看是否会保存价值。 I'm not sure at all how I'm supposed to go about getting this image url. 我完全不确定应该如何获取此图像网址。

This is in ember and I have used the {{action "function name"}} combination in other parts of my code but this is the ONLY part that will not work. 这是在余烬中,我在代码的其他部分中使用了{{action“ function name”}}}组合,但这是唯一无法使用的部分。

Template file 模板文件

<div class="gif-display">
              {{#each giffy as |gif index|}}
                  <img width="150px" {{action "selectGIF" value=gif}} src={{gif.images.original.url}}>
              {{/each}}
</div>

Controller class 控制器类

selectGIF: function (num) {
  console.log("num", num)
  let newPost = this.store.createRecord('post', {
    email: this.get("session.currentUser.email"),
    body: `${gifs[num].images.original.url}`,
    timestamp: new Date().getTime(),
    image: true
  });
  gifs = ''
  newPost.save()
}

And YES I have tested to see if it even runs the function and it does and the images do show up so that's not the problem either. 是的,我已经测试过它是否可以运行该功能,并且确实可以运行图像,因此也不是问题。

My question is, is it possible to pass a parameter into this function. 我的问题是,是否可以将参数传递给此函数。 If not is there a work around? 如果没有解决方法? Should I use a helper function? 我应该使用辅助功能吗?

Thank you! 谢谢!

A function arguments list is an indexed list, rather than a key-value collection. 函数参数列表是索引列表,而不是键值集合。 Because of that it is wrong to pass arguments as key-value pairs. 因此,将参数作为键值对传递是错误的。 Instead, arguments should be given to the helper directly after the action name, as a space-separated list of either references or literal values: 相反,应在操作名称之后直接向助手提供参数,以引用或文字值的空格分隔列表:

<img {{action "selectGIF" gif}} />

Source: https://guides.emberjs.com/v3.0.0/templates/actions/#toc_action-parameters 资料来源: https : //guides.emberjs.com/v3.0.0/templates/actions/#toc_action-parameters


This page also shows that key-value pairs can be given to the action helper, however these are parameters for the helper's execution, instead of the actual action's execution. 此页面还显示可以将键值对提供给操作助手,但是这些是助手执行的参数,而不是实际操作的执行。

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

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