我有這個輸入與ngEnter指令,如果我按下輸入比函數convertAdditionalHoursInHoliday被調用兩次 - 只有第一次。如果我再次按下輸入,則只會調用一次該函數。 有沒有人有任何想法爲什麼或我可以防止這種情況?AngularJS ngEnter - 函數被調用兩次
<input type="text" name="additionalHoursInHolidayAmount"
ng-model="institutionUserConnection.scheduleAbsenceHeader.additionalHoursInHoliday"
ng-Enter="vm.convertAdditionalHoursInHoliday(institutionUserConnection)"
ng-class="institutionUserConnection.scheduleAbsenceHeader.errorTimeAdditionalHoursInHoliday ? 'form-control input errorTime' : 'form-control input'"
placeholder="00:00" />
該指令是這樣的:
(function() {
'use strict';
angular
.module('aposoft.userschedulemanagement')
.directive('ngEnter', function() {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function(){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
})();
非常感謝Vali! – quma
它也適用於我!謝謝! –