Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-maxlength not working for a variable

I'm trying to use ng-maxLength directive to give a variable max-length for my inputs.

function ValidatorCtrl($scope, $timeout) {
  $scope.name = "Star";
  $scope.$metadata={};
  $scope.$metadata.isRequired = true;
  $scope.$metadata.maxLength=5;
}


     <input type="text" ng-model="name" ng-maxlength="$metadata.maxLength"   
ng-required="$metadata.isRequired" name="name" /> 

But it doesn't seems to be working as expected. When it is applied the model binding for the same variable is not working. Here is my plunker

If I remove ng-maxLength ng-required is going to work as expected , otherwise ng-required is also not working.

like image 878
Jayantha Lal Sirisena Avatar asked Apr 02 '14 10:04

Jayantha Lal Sirisena


1 Answers

ng-maxlength does not take scope values.
Traditionally ng-maxlength is used as such ng-maxlength="3". You can either add curly braces around your scope value or create a directive that binds scope value inside ng-maxlength.

like image 146
yangli-io Avatar answered Oct 13 '22 11:10

yangli-io