简体   繁体   English

从Ember.js控制器中的操作获取元素

[英]Get element from the action in a Ember.js controller

How can I get the element from an action in a text area with Ember.js? 如何使用Ember.js从文本区域中的操作获取元素?

This code doesn't work: 该代码不起作用:

App.EnquiryBreakfastController = Ember.ObjectController.extend({
  actions: {
    grow: function(event){
      console.log(this.get('element'));
    }
  }
});
.copy
  %h2 Tell us about your breakfast? 
.enquiry__grid
  {{textarea key-press="grow" value=project placeholder="Water and Bread" class="enquiry--input enquiry--input--area" cols='25' rows='1'}}
.enquiry--button
  {{#link-to 'enquiry.budget' class="btn"}}Next{{/link-to}}

You can't get the element from the controller because it doesn't have any reference to the template. 您无法从控制器获取元素,因为它没有对模板的任何引用。 You would instead need to wrap the textarea in a custom component and bind to the key press event. 相反,您需要将文本区域包装在自定义组件中并绑定到按键事件。

This example is using an ES6 module export since that is the standard now with ember-cli. 此示例使用ES6模块导出,因为这是ember-cli现在的标准。

app/components/grow-textarea.js 应用程序/组件/成长,textarea.js

export default Ember.TextArea.extend({
  grow: function(event) {
    // Your logic here
  }.on('keyPress')
});

Then in your template: 然后在您的模板中:

{{grow-textarea value=project placeholder="Water and Bread" class="enquiry--input enquiry--input--area" cols='25' rows='1'}}

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

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