AngularJS - NG-变化和空格键 [英] AngularJS - ng-change and spacebar
问题描述
我有一个当一个textarea的值更改时调用的函数。它的伟大工程,除了当空格键为pressed,再没有什么被调用。有没有一种方法来激活吗?技术上,内容在不断变化,我想无论函数被调用。
I have a function that is called when a textarea's value is changed. It works great, except when the spacebar is pressed, then nothing is called. Is there a way to activate this? Technically, the content is changing, and I would like the function to be called regardless.
我附上与code小提琴。你可以看到 $ scope.log
数组不压时,空格键为pressed(回车键以及)的新元素,但它与其他任何键不这不是空白。
I am attaching a fiddle with the code. You can see the $scope.log
array does not push a new element when the spacebar is pressed (enter key as well), but it does with any other key that is not white space.
样小提琴: http://jsfiddle.net/UJWLN/4/
推荐答案
您可以使用该指令NG修剪你的输入,并将其设置为false,像这样的:
You can use the directive ng-trim in your input and set it to false, like this:
<textarea ng-change="changeFunction()" ng-model="myModel" ng-trim="false"></textarea>
但是,这不会对任何情况下工作。如果你想成为每一个按键执行的东西,尝试用一个自定义的指令。我写了一个给你:
But this won't work for every case. If you want something to be executed on every single keystroke, try with a custom directive. I wrote one for you:
myApp.directive('ngKeystroke', function(){
return {
restrict: 'A',
link: function(scope, elem, attrs){
elem.bind("keyup", function(){
scope.log.push('called');
scope.$digest();
});
}
};
});
这篇关于AngularJS - NG-变化和空格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!