我在循环中有一个select元素,我想要触发的动作需要有两个参数:select元素的 selected 选项和我们正在循环的项目。我的代码如下所示:
{{#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:function(loopItem, value){
//Do something here with both.
// like loopItem.set('thisProperty', value);
}
因此,在我的特定情况下,子项的数量是动态的,我需要将选定的子项与它当前所在的循环项一起使用。
除非重构为组件(现在,我不能这样做),我如何将多个参数传递给在“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}}>
对于上述所有情况,我在函数中的两个参数都未定义(或导致另一个错误)。唯一有效的是:
<select onChange={{action 'fireThis' value="target.value"}}>
但这只会给我 subItem,而不是 loopItem。
不幸的是,loopItems 是新创建的 Ember 对象,没有任何 ID 参数,所以我也不能给每个选择元素一个唯一的 ID。传递多个参数的能力几乎可以解决我的整个问题。