Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 18f49a5

Browse files
author
Pat Gannon
committed
Add capability to not validate input fields until blur event
1 parent 037aefa commit 18f49a5

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/ng/directive/input.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,33 +406,36 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
406406
}
407407
};
408408

409-
// if the browser does support "input" event, we are fine - except on IE9 which doesn't fire the
410-
// input event on backspace, delete or cut
411-
if ($sniffer.hasEvent('input')) {
412-
element.bind('input', listener);
409+
if (attr.ngUpdateOn) {
410+
element.bind(attr.ngUpdateOn, listener);
413411
} else {
414-
var timeout;
412+
// if the browser does support "input" event, we are fine - except on IE9 which doesn't fire the
413+
// input event on backspace, delete or cut
414+
if ($sniffer.hasEvent('input')) {
415+
element.bind('input', listener);
416+
} else {
417+
var timeout;
415418

416-
element.bind('keydown', function(event) {
417-
var key = event.keyCode;
419+
element.bind('keydown', function(event) {
420+
var key = event.keyCode;
418421

419-
// ignore
420-
// command modifiers arrows
421-
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
422+
// ignore
423+
// command modifiers arrows
424+
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return;
422425

423-
if (!timeout) {
424-
timeout = $browser.defer(function() {
425-
listener();
426-
timeout = null;
427-
});
428-
}
429-
});
426+
if (!timeout) {
427+
timeout = $browser.defer(function() {
428+
listener();
429+
timeout = null;
430+
});
431+
}
432+
});
430433

431-
// if user paste into input using mouse, we need "change" event to catch it
432-
element.bind('change', listener);
434+
// if user paste into input using mouse, we need "change" event to catch it
435+
element.bind('change', listener);
436+
}
433437
}
434438

435-
436439
ctrl.$render = function() {
437440
element.val(isEmpty(ctrl.$viewValue) ? '' : ctrl.$viewValue);
438441
};

test/ng/directive/inputSpec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,24 @@ describe('input', function() {
378378
});
379379

380380

381-
it('should update the model on "blur" event', function() {
381+
it('should update the model on input event', function() {
382382
compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');
383383

384384
changeInputValueTo('adam');
385385
expect(scope.name).toEqual('adam');
386386
});
387387

388388

389+
it('should not update the model until blur event when ng-update-on="blur" set on input element', function() {
390+
compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" ng-update-on="blur" />');
391+
392+
changeInputValueTo('pat');
393+
expect(scope.name).toEqual(undefined);
394+
browserTrigger(inputElm, 'blur');
395+
expect(scope.name).toEqual('pat');
396+
});
397+
398+
389399
it('should update the model and trim the value', function() {
390400
compileInput('<input type="text" ng-model="name" name="alias" ng-change="change()" />');
391401

0 commit comments

Comments
 (0)