【问题标题】:How to listen to a paste event on a textarea in emberjs如何在 emberjs 中的 textarea 上收听粘贴事件
【发布时间】:2014-01-26 22:14:38
【问题描述】:

我有一个包含以下模板的组件:

<div>
  {{textarea value=content autofocus="autofocus"}}
  <button {{action 'cancel'}}>cancel</button>
</div>

如何在我的组件中的这个 textarea 上收听 paste 事件?

我尝试创建一个粘贴操作,但这似乎不起作用:

App.EditableTextComponent = Ember.Component.extend({

  templateName: 'components/editable-text',

  actions: {
    paste: function() {
      console.log(arguments);
    }
  }

});

【问题讨论】:

标签: ember.js


【解决方案1】:

正如@wojciech-bednarski 在他的评论中所建议的,我已将自定义事件侦听器添加到我的应用程序中:

var App = Ember.Application.create({
  customEvents: {
    paste: "paste"
  }
});

然后可以在我的组件中收听它

App.EditableTextComponent = Ember.Component.extend({

  paste: function(event) {
    console.log(event)
  }

});

【讨论】:

    【解决方案2】:

    你可以试试,

    App.EditableTextComponent = Ember.Component.extend({
      didInsertElement:function(){
        this._super();
        var self = this;
        this.$('textarea').on("paste",function(e){
          alert('you just pasted something');
    
    /*to be more accurate when calculating the pasted value, 
    you may need to get the current value of the textarea here 
    and then remove it from the value retrieved inside setTimeout*/
    
          setTimeout(function () { 
    /*the paste event is fired before the value has already been set, 
    so this is handled here. 
    Also mentioned here as a hacky solution, http://stackoverflow.com/questions/9494283/jquery-how-to-get-the-pasted-content*/
            alert("pasted:"+self.$('textarea').val()); 
        }, 100);
        });
      },
      templateName: 'components/editable-text'
    });
    

    http://emberjs.jsbin.com/EHejUfuj/1/edit

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 2016-11-12
      • 2012-01-19
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多