gpt4 book ai didi

javascript - AngularJS:在编写韩文字符时触发 ng-change、ng-keyup 或 $scope.$watch

转载 作者:数据小太阳 更新时间:2023-10-29 04:36:52 25 4
gpt4 key购买 nike

我正在构建一个韩语词汇训练器,我想将用户输入与人们输入的内容进行比较。

在韩语和其他一些亚洲语言中,您可以通过多个按键事件来组合字母。在 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 :

.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: 감

最佳答案

正如 Angular 团队的一位乐于助人的成员所解释的那样,在组合 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();
});
}
};
});
})();

Controller :(按照 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
});
});
}
};
});

})();

Controller :

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>

关于javascript - AngularJS:在编写韩文字符时触发 ng-change、ng-keyup 或 $scope.$watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com