简体   繁体   English

Ember JS,将输入标签值作为参数传递给操作把手

[英]Ember JS,Pass input tag value as parameter in action handlbars

I want to get value from <input> tag and pass it as parameter to action in EmberJS 我想从<input>标记获取值并将其作为参数传递给EmberJS中的操作

When I used JQuery {{action 'add' this.$('#x').val()}} Ember failed to build giving parse error on line ... 当我使用JQuery {{action 'add' this.$('#x').val()}} action'add'this {{action 'add' this.$('#x').val()}} Ember无法建立并parse error on line ...给出parse error on line ...

Here I want to pass x and y as parameter to the {{action}} , I know the syntax for sending parameters is {{action parameter1 parameter2 ..etc}} 在这里,我想将x和y作为参数传递给{{action}} ,我知道发送参数的语法是{{action parameter1 parameter2 ..etc}}

templates\\alpha.hbs 模板\\ alpha.hbs

<h1>alpha template</h1>
<div>
 <label>X value</label>
 <input type="text" id="x">
 <label>Y</label>
 <input type="text" id="y">
 <input type="button" id ="add-button" value="Add" {{action 'add' }}>
</div>

controllers\\alpha.js 控制器\\ alpha.js

import Ember from 'ember';

export default Ember.Controller.extend({
  actions:{
    add: function(x, y){
      alert('this is done right '+x+ ' ' + y);
    }
  }
});

I tested the code without passing parameters and the code worked as expected giving the alert required 我在不传递参数的情况下测试了代码,并且代码按预期方式工作,给出了所需的警报

You need to take advantage of input helpers and value binding in Ember: 您需要利用Ember中的input助手和值绑定:

<br><h1>alpha template</h1>
<div>
 <label>X value</label>
 {{input value=xValue}}
 <label>Y</label>
 {{input value=yValue}}
 <input type="button" id ="add-button" value="Add" {{action 'add' xValue yValue}}/>
</div>

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

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