如何在Ember2.3中为select元素的onChange动作传递多个参数 [英] How do I pass in more than one parameter to the onChange action for the select element in Ember2.3
问题描述
我在循环中有一个 select 元素,我要触发的动作需要具有两个参数: select 元素的selected选项和该项我们正在循环.我的代码如下:
I have a select element within a loop, and the action I want to be triggered needs to have two parameters: the selected option of the select element and the item we are looping with. My code looks like so:
{{#each loopItems as |loopItem|}}
<select onChange={{action 'fireThis' loopItem target.value }}>
{{#each loopItem.subItems as |subItem|}}
<option value={{subItem.id}}>{{subItem.value}}</option>
{{/each}}
</select>
{{/each}}
我的 fireThis 操作如下:
fireThis:function(loopItem, value){
//Do something here with both.
// like loopItem.set('thisProperty', value);
}
因此,在我的特殊情况下,子项的数量是动态的,我需要将选定的子项与当前所在的loopItem一起使用.
So, in my particular case, the number of subItems is dynamic, and I need to use the selected subItem with the loopItem it is currently under.
除非将其重构到组件中(现在,我不能这样做),我如何将多个参数传递给在"onChange"上触发的操作?
Barring refactoring into components (right now, I cannot do that), how would I pass in more than one parameter to the action fired on 'onChange'?
我尝试过:
<select onChange={{action 'fireThis' value ="loopItem target.value" }}>
<select onChange={{action 'fireThis' value ="loopItem target.value" }}>
<select onChange={{action 'fireThis' loopItem value="target.value"}}>
<select onChange={{action 'fireThis' value="target.value" loopItem}}>
对于以上所有内容,我在函数中的两个参数均未定义(或导致另一个错误).唯一可行的是:
For all of the above, both my params in the function are undefined (or cause another error). The only one that works is:
<select onChange={{action 'fireThis' value="target.value"}}>
但这只给了我subItem,而不是loopItem.
But that only gives me the subItem, not the loopItem.
不幸的是,loopItems是新创建的Ember对象,没有任何ID参数,因此我也不能给每个选择元素一个唯一的ID.传递多个参数的能力几乎可以解决我的整个问题.
Unfortunately, the loopItems are newly created Ember Objects, void of any ID parameters, so I cannot give each select element a unique ID either. The ability to pass more than one parameter would pretty much solve my entire problem.
推荐答案
有点晚了,但是在这里找到的答案有效: 在选择菜单的操作中前2个参数.
A bit late, but the answer found here works: ember 2 parameters in a action of a select menu.
对于OP的解决方案,您可以像这样包装它:
For OP's solution you wrap it like so:
<select onChange={{action (action 'fireThis' loopItem) value="target.value"}}>
这篇关于如何在Ember2.3中为select元素的onChange动作传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!