簡體   English   中英

Ember JS,將輸入標簽值作為參數傳遞給操作把手

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

我想從<input>標記獲取值並將其作為參數傳遞給EmberJS中的操作

當我使用JQuery {{action 'add' this.$('#x').val()}} action'add'this {{action 'add' this.$('#x').val()}} Ember無法建立並parse error on line ...給出parse error on line ...

在這里,我想將x和y作為參數傳遞給{{action}} ,我知道發送參數的語法是{{action parameter1 parameter2 ..etc}}

模板\\ 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>

控制器\\ alpha.js

import Ember from 'ember';

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

我在不傳遞參數的情況下測試了代碼,並且代碼按預期方式工作,給出了所需的警報

您需要利用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