i have template
below inside directive
<input ng-model="acc" type="number" required /> <p class="input-details" ng-class="{'has-error': confirm(acc, 3, 17)}"></p>
i have method in directive controller
function
$scope.confirm= function(value, min, max) { if (!value) return false; return ("" + value).length < min || ("" + value).length > max; };
the value passed undefined
in confirm
method, when call same method in ng-change
of input below, getting passed.
<input ng-model="acc" type="number" ng-change="confirm(acc,3,17)" required />
why ng-class
not getting correct ng-model
value?
(""+3).length = 1 || (""+17).length = 2
because of type conversion see codepen example crank number input , see (""+acc).length equals , how fix https://codepen.io/anon/pen/vkqzor
app.controller('classtesterctrl', function($scope, $log){ $scope.confirm= function(value, min, max) { if($scope.acc < 3 || $scope.acc > 17){ $scope.valid = false; } else { $scope.valid = true; } }; }); <div ng-app="modeltest"> <div ng-controller="classtesterctrl"> <input ng-model="acc" type="number" ng-change="confirm()" required /> <p class="input-details" ng-class="{'has-error': valid}"></p> {{(""+acc).length}} {{acc}} {{valid}} </div> </div>
Comments
Post a Comment