Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
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
5 changes: 4 additions & 1 deletion src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ function mdMaxlengthDirective($animate, $mdUtil) {
var ngTrim = angular.isDefined(attr.ngTrim) ? $mdUtil.parseAttributeBoolean(attr.ngTrim) : true;
var isPasswordInput = attr.type === 'password';

scope.$watch(attr.mdMaxlength, function(value) {
maxlength = value;
});

ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
if (!angular.isNumber(maxlength) || maxlength < 0) {
return true;
Expand Down Expand Up @@ -685,7 +689,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
});

scope.$watch(attr.mdMaxlength, function(value) {
maxlength = value;
if (angular.isNumber(value) && value > 0) {
if (!charCountEl.parent().length) {
$animate.enter(charCountEl, errorsSpacer);
Expand Down
36 changes: 36 additions & 0 deletions src/components/input/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,42 @@ describe('md-input-container directive', function() {
expect(getCharCounter(el).text()).toBe('5 / 5');
});

it('should error with an interpolated value and incorrect initial value', function() {
var el = $compile(
'<form name="form">' +
' <md-input-container>' +
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
' </md-input-container>' +
'</form>')(pageScope);

pageScope.$apply('mymax = 8');
pageScope.$apply('foo = "ABCDEFGHIJ"');

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

expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
expect(getCharCounter(el).text()).toBe('10 / 8');
});

it('should work with an interpolated value and correct initial value', function() {
var el = $compile(
'<form name="form">' +
' <md-input-container>' +
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
' </md-input-container>' +
'</form>')(pageScope);

pageScope.$apply('mymax = 5');
pageScope.$apply('foo = "abcde"');

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

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

it('should work with a constant', function() {
var el = $compile(
'<form name="form">' +
Expand Down