在选择菜单的动作中嵌入2个参数 [英] ember 2 parameters in a action of a select menu

查看:85
本文介绍了在选择菜单的动作中嵌入2个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在余烬动作中传递多个参数. 问题在于参数之一是value ='target.value',显然Ember不喜欢添加第二个参数.

I'm trying to pass more than one argument in an ember action. The problem is that one of the arguments is value='target.value' and apparently Ember doesn't like to add a second one.

如何传递这两个参数? 如果我只需要一个参数,则可以使用value ='target.value'.

How can I pass these two parameters? value='target.value' works if I only need one argument.

我尝试用这种方式编写它,但是类别未定义.

I've tried to write it this way but category is undefined.

{{#each selectorsData as |selectorItem|}}
<select onchange={{ action selectorItem.action value='target.value' category='selectorItem.name' }}>

我也尝试过这种方法,但是Ember给了我一个错误

I've tried this way too, but Ember gives me an error

{{#each selectorsData as |selectorItem|}}
   <select onchange={{ action selectorItem.action value='target.value' selectorItem.name }}>

 Error: Parse error on line 4:
 ....value' selectorItem.name }}>

推荐答案

为此,您必须使用闭包操作. 引用调用动作

To accomplish this you must use closure actions. Reference to Invoking Action

控制器

export default Ember.Controller.extend({
  appName: 'Ember Twiddle',
  selectorsData: [
    { name: 'mike', value: '1', action: 'alert', options: ["1", "2"] },
    { name: 'steve', value: '2', action: 'alert', options: ["1", "2"]  }
  ],

  actions: {
    alert(value, name, target) {
      alert("Hello: " + value + " - " + name + "-" + target);
    }
  }

});

模板

<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{#each selectorsData as |selectorItem|}}
    <label>{{selectorItem.name}}</label>
   <select onchange={{action (action selectorItem.action selectorItem.value selectorItem.name) value="target.value"}}>
        {{#each selectorItem.options as |option|}}
        <option value={{option}}>{{option}}</option
      {{/each}}
   </select>
   <br>
{{/each}}

旋转

这篇关于在选择菜单的动作中嵌入2个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆