我正在建立一名韩语词汇训练师,我希望将用户输入与人们输入进行比较。
在韩语和其他一些亚洲语言中,您使用多个键盘事件撰写字母。在Chrome中,$ scope。$ watch,ng-keyup和ng-change仅在字母完全合成并且输入了新字母或空格后触发。我不介意AngularJS在最后一个字母完全合成之前没有触发任何东西,但是一旦字母完成,它应该触发而不必添加空格或开始下一个字。
HTML:
<form name="forms.vocabularyForm">
<input name="answer" id="answer" ng-model="vocabularyCtrl.answer" ng-change="vocabularyCtrl.checkChange()" ng-keyup="vocabularyCtrl.checkKeyUp($event)" type="text" />
</form>
控制器:
.controller('VocabularyCtrl', [
'$scope',
'$location',
function($scope, $location) {
this.checkChange = function () {
console.log("answer change: " + this.answer);
};
this.checkKeyUp = function ($event) {
console.log("answer keyUp: " + this.answer);
};
$scope.$watch('vocabularyCtrl.answer', function (answerNew, answerOld) {
console.log('answerOld: ' + answerOld + ', answerNew: ' + answerNew);
}, true);
};
]);
示例:
Input: ㄱ
Console:
answerOld: , answerNew:
answer keyUp:
Input: 가
Console:
answerOld: , answerNew:
answer keyUp:
Input: 감 (character is now fully composed)
Console:
answerOld: , answerNew:
answer keyUp:
Input: 감ㅅ (starting the next character, same behaviour with space bar)
Console:
answerOld: 감, answerNew:
answer change: 감
answer keyUp: 감
答案 0 :(得分:4)
正如Angular团队的一位乐于助人的成员所解释的那样,在编写角色时会故意抑制所有触发器。更多详情here。
根据建议,我创建了一个自定义指令,在编写字符时手动更新模型:
指令:
(function() {
'use strict';
angular.module('myApp', [])
// Angular's ng-change, ng-keyup and $scope.$watch don't get triggered
// while composing (e.g. when writing Korean syllables).
// See: https://github.com/angular/angular.js/issues/10588
// This custom directive uses element.on('input') instead, which gets
// triggered while composing.
.directive('cstInput', function() {
return {
restrict: 'A',
require: '^ngModel',
scope: {
ngModel: '=', // sync model
},
link: function (scope, element, attrs, ngModel) {
element.on('input', function() {
scope.ngModel = element.val();
});
}
};
});
})();
控制器:(由ippi建议)
$scope.$watch('quizzesCtrl.answer', function (answer) {
console.log(answer);
});
HTML:
<form ng-controller="QuizzesController as quizzesCtrl">
<input cst-input name="answer" id="answer" ng-model="quizzesCtrl.answer" type="text" />
</form>
<强>更新强>
我必须将代码更改为以下代码才能使其在FireFox中正常工作(Chrome和Safari可以正常使用上面的代码)。
指令:
(function() {
'use strict';
angular.module('myApp', [])
// Angular's ng-change, ng-keyup and $scope.$watch don't get triggered
// while composing (e.g. when writing Korean syllables).
// See: https://github.com/angular/angular.js/issues/10588
// This custom directive uses element.on('input') instead, which gets
// triggered while composing.
.directive('cstInput', function() {
return {
restrict: 'A',
require: '^ngModel',
link: function (scope, element, attrs, ngModel) {
element.on('input', function() {
scope.$apply(function(){
scope.ngModel = element.val();
scope.$eval(attrs.cstInput, {'answer': scope.ngModel}); // only works if no scope has been defined in directive
});
});
}
};
});
})();
控制器:
this.checkAnswer = function (answer) {
if (answer === this.quiz.answer) {
this.isCorrect = true;
}
};
HTML(请注意,任何传入的参数都需要在cst-input-callback中列出):
<form ng-controller="QuizzesController as quizzesCtrl">
<input cst-input="quizzesCtrl.checkAnswer(answer)" ng-model="quizzesCtrl.answer" name="answer" id="answer" type="text" />
</form>
答案 1 :(得分:3)
我在Angular.js 1.2.27中发现了这个错误,我尝试了其他版本,但我没有遇到任何问题。但我找到了解决方案,这将解决您的问题。
看看这个解决方案 https://github.com/mbenford/ngTagsInput/issues/303
angular.module('angularApp')
.directive('ignoreCompositionEvent', function () {
return {
restrict: 'A',
link: function postLink(scope, element) {
//element.val('this is the ignoreCompositionEvent directive');
element.off('compositionstart').off('compositionend');
}
};
});
这是example。只需打开您的控制台并在输入中键入한<。
答案 2 :(得分:1)
使用$watch
,您将能够捕获模型的所有更新:
工作示例(jsfiddle):
<div ng-app ng-controller="myctrl">
<input type="text" ng-model="answer" />
</div>
function myctrl($scope) {
$scope.$watch('answer',function(oldVal,newVal){
console.log(oldVal,newVal);
});
}
供参考,看起来可以使用ng-model-options并将compositionupdate
- 事件分配给updateOn
。我没有运气,而是使用$ watch。