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

Commit 198de5c

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 f5af7a1 commit 198de5c

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

522-
ngModelCtrl.$formatters.push(renderCharCount);
523-
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
524-
element.on('input keydown keyup', function() {
525-
renderCharCount(); //make sure it's called with no args
526-
});
527-
528522
scope.$watch(attr.mdMaxlength, function(value) {
529523
maxlength = value;
530524
if (angular.isNumber(value) && value > 0) {
@@ -541,6 +535,11 @@ function mdMaxlengthDirective($animate, $mdUtil) {
541535
if (!angular.isNumber(maxlength) || maxlength < 0) {
542536
return true;
543537
}
538+
539+
// We always update the char count, when the modelValue has changed.
540+
// Using the $validators for triggering the update works very well.
541+
renderCharCount();
542+
544543
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
545544
};
546545
});

src/components/input/input.spec.js

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

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

0 commit comments

Comments
 (0)