ember 2 parameters in a action of a select menu

岁酱吖の 提交于 2019-12-13 01:26:40

问题


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.

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' }}>

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 }}>

回答1:


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

Controller

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);
    }
  }

});

Template

<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}}

Twiddle




回答2:


Try something like this:

<select onchange={{action selectorItem.action target.value selectorItem.name}}>

....

yourAction: function(targetValue, selectorItemName) {..}

Here's an example: http://jsfiddle.net/nvfm3yz1/



来源:https://stackoverflow.com/questions/36291998/ember-2-parameters-in-a-action-of-a-select-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!