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

Commit bf5c036

Browse files
devversionkara
authored andcommitted
fix(input): md-maxlength not properly updates on model changes. (#8351)
* Currently the md-maxlength char counter was not updated properly and listening on the different events is unnecessary. ngModelController will already update the model, when the `input` event gets called. Additonally we can cover all updates in the $validator itself, because it will always update, when changes get applied to the model. Fixes #1870.
1 parent c9a215b commit bf5c036

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/components/input/input.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
637637
// over the maxlength still counts as invalid.
638638
attr.$set('ngTrim', 'false');
639639

640-
ngModelCtrl.$formatters.push(renderCharCount);
641-
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
642-
element.on('input keydown keyup', function() {
643-
renderCharCount(); //make sure it's called with no args
644-
});
645-
646640
scope.$watch(attr.mdMaxlength, function(value) {
647641
maxlength = value;
648642
if (angular.isNumber(value) && value > 0) {
@@ -659,6 +653,11 @@ function mdMaxlengthDirective($animate, $mdUtil) {
659653
if (!angular.isNumber(maxlength) || maxlength < 0) {
660654
return true;
661655
}
656+
657+
// We always update the char count, when the modelValue has changed.
658+
// Using the $validators for triggering the update works very well.
659+
renderCharCount();
660+
662661
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
663662
};
664663
});

src/components/input/input.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,33 @@ describe('md-input-container directive', function() {
306306
expect(getCharCounter(element).text()).toBe('3 / 6');
307307
});
308308

309+
it('should update correctly the counter, when deleting the model value', function() {
310+
var el = $compile(
311+
'<form name="form">' +
312+
'<md-input-container>' +
313+
'<input md-maxlength="5" ng-model="foo" name="foo">' +
314+
'</md-input-container>' +
315+
'</form>'
316+
)(pageScope);
317+
318+
pageScope.$apply();
319+
320+
// Flush any pending $mdUtil.nextTick calls
321+
$timeout.flush();
322+
323+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
324+
expect(getCharCounter(el).text()).toBe('0 / 5');
325+
326+
pageScope.$apply('foo = "abcdef"');
327+
expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
328+
expect(getCharCounter(el).text()).toBe('6 / 5');
329+
330+
331+
pageScope.$apply('foo = ""');
332+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
333+
expect(getCharCounter(el).text()).toBe('0 / 5');
334+
});
335+
309336
it('should add and remove maxlength element & error with expression', function() {
310337
var el = $compile(
311338
'<form name="form">' +

0 commit comments

Comments
 (0)