javascript - Delay on Form setvalidity in AngularJS -


i using directive on setting form validity. works accordingly boolean value of custom validity not return right away , exact needs keypress. way i'm using 'element.blur' setting custom validity.

here html code:

<!doctype html> <html ng-app="myapp">    <head>     <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">     <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.min.css">     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>     <script type="text/javascript" src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>     <script src="script.js"></script>   </head>    <body>       <form ng-controller="myctrl" name="biomanagementform" novalidate>         <fieldset ng-repeat="bioeducation in bioeducations" data-ng-form="nestedbioeducationsform">           degree: <br />           availability: -- {{ nestedbioeducationsform.bio_education_degree.$error.available }}           <input class="form-control input-form" ng-model="bioeducation.bio_education_degree" name="bio_education_degree" id="bio_education_degree" auto-complete-validation ui-items="searchdegreeitems">           <span class="errors" id="error-bio-education-degree">                         <span ng-if="nestedbioeducationsform.bio_education_degree.$error.available"> * not available <br /></span>                     </span>                     school: <br />                     availability: -- {{ nestedbioeducationsform.bio_education_school.$error.available }}                     <input class="form-control input-form" type="text" ng-model="bioeducation.bio_education_school" name="bio_education_school" id="bio_education_school" auto-complete-validation ui-items="searchschoolitems">           <span class="errors" id="error-bio-education-school">                         <span ng-if="nestedbioeducationsform.bio_education_school.$error.available"> * not available <br /></span>                     </span>         </fieldset>     </form>   </body> </html> 

here js code:

// code goes here  var myapp = angular.module('myapp', []);    myapp.controller('myctrl', function($scope){   $scope.name = "dean";   $scope.originalbioeducations = [{}];     $scope.bioeducations = angular.copy($scope.originalbioeducations);     $scope.searchschoolitems = ["don bosco", "jru", "ust", "feu"];     $scope.searchdegreeitems = ["bsit", "bsed", "ece", "coe"]; });  function monkeypatchautocomplete() {     // don't need save old fn,      // chain if wanted     var oldfn = $.ui.autocomplete.prototype._renderitem;      $.ui.autocomplete.prototype._renderitem = function( ul, item) {         var re = new regexp( "(" + this.term + ")", "gi" );         var t = item.label.replace(re,"<span style='font-weight:bold;color:#04508e;'>" + this.term + "</span>");         return $( "<li></li>" )         .data( "item.autocomplete", item )         .append( "<a>" + t + "</a>" )         .appendto( ul );     }; }  monkeypatchautocomplete(); function remove_duplicates_safe(arr) {         var obj = {};         var arr2 = [];         (var = 0; < arr.length; i++) {             if (!(arr[i] in obj)) {                 arr2.push(arr[i]);                 obj[arr[i]] = true;             }         }         return arr2;     }   myapp.directive('autocompletevalidation', function($timeout){         return {             require: 'ngmodel',             scope: {                 uiitems: "="             },             link: function(scope, element, attrs, ctrl){                 scope.$watchcollection('uiitems', function(val){                     items = scope.uiitems.filter(function(n){ return n != undefined });                     element.autocomplete({                         source: remove_duplicates_safe(items),                         minlength:2,                     });                     element.bind('blur', function(){                         _val = element.val();                         _index = items.indexof(_val);                         if(_index == -1){                             ctrl.$setvalidity('available', false);                             console.log("dean");                         }else{                             ctrl.$setvalidity('available', true);                             console.log("armada");                         }                     });                 });             }         }     }); 

p.s.

the app dynamic field via ng-repeat. using data-ng-form dynamic validations well. both input fields run jquery ui autocomplete. validation should detect if value on field in autocomplete choices inside array(the 1 "items" scope variable). should throw error if 1 on input field not in choices.

here example in plunkr: http://plnkr.co/edit/2epuhrir9oncv8z7q018?p=preview

if want avoid adding keypress event should use $validators object property on ngmodelcontroller. correct way create validator directive anyway. might want add change event autocomplete, can $setviewvalue.

               scope.$watchcollection('uiitems', function(val){                 items = scope.uiitems.filter(function(n){ return n != undefined });                 element.autocomplete({                     source: remove_duplicates_safe(items),                     minlength:2,                     change: function () {                       ctrl.$setviewvalue(element.val());                     }                 });             });              ctrl.$validators.available = function (modelvalue, viewvalue) {               var value = modelvalue || viewvalue;               if (items) {               var _index = items.indexof(value);                return _index !== -1;               }             } 

plunker: http://plnkr.co/edit/pcarndakheouqnciq2yt?p=preview

btw. don't create global variables on fly, adding _ @ beging of name not make them local or private.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -