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

Commit 2c27992

Browse files
committed
fix(input): md-maxlength not properly updates on model changes.
* 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 da54670 commit 2c27992

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
@@ -620,12 +620,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
620620
// over the maxlength still counts as invalid.
621621
attr.$set('ngTrim', 'false');
622622

623-
ngModelCtrl.$formatters.push(renderCharCount);
624-
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
625-
element.on('input keydown keyup', function() {
626-
renderCharCount(); //make sure it's called with no args
627-
});
628-
629623
scope.$watch(attr.mdMaxlength, function(value) {
630624
maxlength = value;
631625
if (angular.isNumber(value) && value > 0) {
@@ -642,6 +636,11 @@ function mdMaxlengthDirective($animate, $mdUtil) {
642636
if (!angular.isNumber(maxlength) || maxlength < 0) {
643637
return true;
644638
}
639+
640+
// We always update the char count, when the modelValue has changed.
641+
// Using the $validators for triggering the update works very well.
642+
renderCharCount();
643+
645644
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
646645
};
647646
});

src/components/input/input.spec.js

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

297+
it('should update correctly the counter, when deleting the model value', function() {
298+
var el = $compile(
299+
'<form name="form">' +
300+
'<md-input-container>' +
301+
'<input md-maxlength="5" ng-model="foo" name="foo">' +
302+
'</md-input-container>' +
303+
'</form>'
304+
)(pageScope);
305+
306+
pageScope.$apply();
307+
308+
// Flush any pending $mdUtil.nextTick calls
309+
$timeout.flush();
310+
311+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
312+
expect(getCharCounter(el).text()).toBe('0/5');
313+
314+
pageScope.$apply('foo = "abcdef"');
315+
expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
316+
expect(getCharCounter(el).text()).toBe('6/5');
317+
318+
319+
pageScope.$apply('foo = ""');
320+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
321+
expect(getCharCounter(el).text()).toBe('0/5');
322+
});
323+
297324
it('should add and remove maxlength element & error with expression', function() {
298325
var el = $compile('<form name="form">' +
299326
' <md-input-container>' +

0 commit comments

Comments
 (0)