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);
});
任何提示將不勝感激。提前致謝。
謝謝你的回答。我的最終目標是捕獲的[oninput](http://www.w3schools.com/jsref/event_oninput.asp)事件和的更改事件與ember.js。這些事件可以通過jQuery或普通的javascript來捕捉。 – Calvin
我會嘗試使用計算的屬性或觀察者。 Kingpin2k,謝謝! – Calvin