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

fix(input): md-maxlength not properly updates on model changes. #8351

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
// over the maxlength still counts as invalid.
attr.$set('ngTrim', 'false');

ngModelCtrl.$formatters.push(renderCharCount);
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
element.on('input keydown keyup', function() {
renderCharCount(); //make sure it's called with no args
});

scope.$watch(attr.mdMaxlength, function(value) {
maxlength = value;
if (angular.isNumber(value) && value > 0) {
Expand All @@ -642,6 +636,11 @@ function mdMaxlengthDirective($animate, $mdUtil) {
if (!angular.isNumber(maxlength) || maxlength < 0) {
return true;
}

// We always update the char count, when the modelValue has changed.
// Using the $validators for triggering the update works very well.
renderCharCount();

return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
};
});
Expand Down
27 changes: 27 additions & 0 deletions src/components/input/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,33 @@ describe('md-input-container directive', function() {
expect(getCharCounter(element).text()).toBe('3/6');
});

it('should update correctly the counter, when deleting the model value', function() {
var el = $compile(
'<form name="form">' +
'<md-input-container>' +
'<input md-maxlength="5" ng-model="foo" name="foo">' +
'</md-input-container>' +
'</form>'
)(pageScope);

pageScope.$apply();

// Flush any pending $mdUtil.nextTick calls
$timeout.flush();

expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
expect(getCharCounter(el).text()).toBe('0 / 5');

pageScope.$apply('foo = "abcdef"');
expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
expect(getCharCounter(el).text()).toBe('6 / 5');


pageScope.$apply('foo = ""');
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
expect(getCharCounter(el).text()).toBe('0 / 5');
});

it('should add and remove maxlength element & error with expression', function() {
var el = $compile('<form name="form">' +
' <md-input-container>' +
Expand Down