angularjs - Angular validator makes my $scope unavailable? -
i've simple input in html page :
<input type="text" class='form-control' id='card' name="mycard" ng-model='form.cardnb' card-validation>
i created function 'cadtype' returns value according first char in input :
var mainapp = angular.module("mainapp", []); mainapp.controller('formcontroller', function($scope) { $scope.form = { cardnb: "", nameoncard: "", expirydate: "", cvv: "", cardtype: function() { var firstcharcard; var secondcharcard; firstcharcard = $scope.form.cardnb.charat(0); secondcharcard = $scope.form.cardnb.charat(1); //some additional logic here...
(function works fine @ moment , no problem)
then, created new validator validation on input :
mainapp.directive('cardvalidation', function() { return { require: 'ngmodel', link: function($scope, element, attributes, ngmodel) { ngmodel.$validators.cardvalidation = function(value) { return (value.length == 13); }; } } });
this validation works function doesn't work anymore ! error when filling input :
typeerror: cannot read property 'charat' of undefined @ object.$scope.form.cardtype (index.html:86) @ ib.functioncall (angular.js:12404) @ object.<anonymous> (angular.js:12813) @ n.$get.n.$digest (angular.js:14300) @ n.$get.n.$apply (angular.js:14571) @ eg.$$debounceviewvaluecommit (angular.js:23391) @ eg.$setviewvalue (angular.js:23363) @ htmlinputelement.l (angular.js:19784) @ htmlinputelement.c (angular.js:3032)
i suppose validation makes unavailable scope ... can ?
thanks lot :).
how validators work create model if validation fails.
in case if length not 13 underlying model cleared form.cardnb
.
so in function should explicitly check cardnb
not null before using charat.
Comments
Post a Comment