Skip to content

Commit f329004

Browse files
committed
fix(NgModel): make ngMinlength and ngMaxlength as standalone directives
Fixes angular#6750
1 parent 0890edb commit f329004

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/AngularPublic.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
ngChangeDirective,
4646
requiredDirective,
4747
requiredDirective,
48+
minlengthDirective,
49+
maxlengthDirective,
4850
ngValueDirective,
4951
ngModelOptionsDirective,
5052
ngAttributeAliasDirectives,
@@ -184,6 +186,8 @@ function publishExternalAPI(angular){
184186
ngChange: ngChangeDirective,
185187
required: requiredDirective,
186188
ngRequired: requiredDirective,
189+
ngMinlength: minlengthDirective,
190+
ngMaxlength: maxlengthDirective,
187191
ngValue: ngValueDirective,
188192
ngModelOptions: ngModelOptionsDirective
189193
}).

src/ng/directive/input.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -1011,22 +1011,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
10111011
};
10121012
}
10131013
}
1014-
1015-
// min length validator
1016-
if (attr.ngMinlength) {
1017-
var minlength = int(attr.ngMinlength);
1018-
ctrl.$validators.minlength = function(value) {
1019-
return ctrl.$isEmpty(value) || value.length >= minlength;
1020-
};
1021-
}
1022-
1023-
// max length validator
1024-
if (attr.ngMaxlength) {
1025-
var maxlength = int(attr.ngMaxlength);
1026-
ctrl.$validators.maxlength = function(value) {
1027-
return ctrl.$isEmpty(value) || value.length <= maxlength;
1028-
};
1029-
}
10301014
}
10311015

10321016
function weekParser(isoWeek) {
@@ -2134,6 +2118,33 @@ var requiredDirective = function() {
21342118
};
21352119

21362120

2121+
var maxlengthDirective = function() {
2122+
return {
2123+
require: '?ngModel',
2124+
link: function(scope, elm, attr, ctrl) {
2125+
if (!ctrl) return;
2126+
var maxlength = int(attr.ngMaxlength);
2127+
ctrl.$validators.maxlength = function(value) {
2128+
return ctrl.$isEmpty(value) || value.length <= maxlength;
2129+
};
2130+
}
2131+
};
2132+
};
2133+
2134+
var minlengthDirective = function() {
2135+
return {
2136+
require: '?ngModel',
2137+
link: function(scope, elm, attr, ctrl) {
2138+
if (!ctrl) return;
2139+
var minlength = int(attr.ngMinlength);
2140+
ctrl.$validators.minlength = function(value) {
2141+
return ctrl.$isEmpty(value) || value.length >= minlength;
2142+
};
2143+
}
2144+
};
2145+
};
2146+
2147+
21372148
/**
21382149
* @ngdoc directive
21392150
* @name ngList

0 commit comments

Comments
 (0)