2016-02-11 35 views
3

繼ember.js 2.3.0指南的例子, EMBER.TEMPLATES.HELPERS CLASSember.js:爲什麼on-input事件不起作用?

export default Ember.Component.extend({ 
    actions: { 
    // Usage {{input on-input=(action (action 'setName' model) value="target.value")}} 
    setName(model, name) { 
     model.set('name', name); 
    } 
    } 
}); 

我寫我的代碼如下:

template.hbs

<div> 
{{input type='text' id='Txt01' value='firstName' 
    on-input=(action "onInput") 
    key-press=(action "onKeyPress") 
    key-down=(action "onKeyDown") 
    key-up=(action "onKeyUp") 
    change=(action "onChange")}} 
</div> 

控制器。 js

export default Ember.Controller.extend({ 
    actions: { 
    onKeyPress() { 
     console.log('Text-Key-Pressed'); 
    }, 
    onKeyDown() { 
     console.log('Text-Key-Down'); 
    }, 
    onKeyUp() { 
     console.log('Text-Key-Up'); 
    }, 
    onInput() { 
     var value = Ember.$('#Txt01').val(); 
     console.log('onInput:' + value); 
    }, 
    onChange() { 
     var value = Ember.$('#Txt01').val(); 
     console.log('onInput:' + value); 
    } 
    } 
}); 

運行代碼,日誌是

Text-Key-Down 
Text-Key-Pressed 
Text-Key-Up 

兩個oninput事件和改變事件時,輸入的值被改變不起作用。

我只想用ember.js實現以下功能。

<input type="text" id="text1" value="hello!"> 
document.getElementById('text1').addEventListener('input', function(){ 
    var temp = $('#text1').val(); 
    console.log('Text1: ' + temp); 
}, false); 

$('#text1').bind('input propertychange', function() { 
    var temp = $('#text1').val(); 
    console.log('Text1: ' + temp); 
}); 

任何提示將不勝感激。提前致謝。

回答

1

從文檔http://emberjs.com/api/classes/Ember.Templates.helpers.html#method_input

助手允許一些用戶事件發送操作。

  • enter
  • insert-newline
  • escape-press
  • focus-in
  • focus-out
  • key-press
  • key-up

計算屬性或綁定到輸入的屬性的觀察者在任何時候發生更改時都會觸發。我的回答很大程度上取決於你的最終目標,即改變時你想要發生什麼。

+0

謝謝你的回答。我的最終目標是捕獲的[oninput](http://www.w3schools.com/jsref/event_oninput.asp)事件和的更改事件與ember.js。這些事件可以通過jQuery或普通的javascript來捕捉。 – Calvin

+0

我會嘗試使用計算的屬性或觀察者。 Kingpin2k,謝謝! – Calvin

相關問題