angularjs - Retrieved data in AngularsJS and Directive for checking if a username exists -
i follow solution checking if username present in db: defined in topic "angularjs: custom directive check if username exists"
in case, put in practice when user wants update data; script has launched when user wants change lastname.
at beginning, script should not display error message
here my controller:
// directive checking if username used app.directive('usernameavailable', function($timeout, $q, $http, contactservice) { return { restrict: 'ae', require: 'ngmodel', link: function(scope, elm, attr, model) { model.interation=0; model.$asyncvalidators.usernameexists = function() { name= elm.val(); return contactservice.searchcontactbyname(name).success(function(contact){ $timeout(function(){ if(contact.length >0){ model.$setvalidity('usernameexists', contact); //model.$setvalidity('unique', false); // validation criteria in form scope.alreadyexist='true'; scope.contacts = contact; }else{ model.$setvalidity('usernameexists', contact); //model.$setvalidity('unique', true); // validation criteria in form scope.alreadyexist='false'; scope.contacts = null; } }, 600); }); }; } } }); app.controller('ctrleditcontacts', function ($scope, $routeparams, contactservice){ // load data user contactservice.loadpersonbyid($routeparams.contactid).success(function(contact){ $scope.contact.id = contact[0].id; $scope.contact.lastname = contact[0].lastname; $scope.contact.firstname = contact[0].firstname; }); });
here my template:
<div class="panel-body"> <form name="contactform" class="form-horizontal" role="form" novalidate ng-submit="submitform(contact)"> <div class="form-group"> <label for="username" class="col-sm-2 control-label">last name *</label> <div class="col-sm-10"> <input type="text" class="form-control" name="username" maxlength="100" placeholder="enter last name" required ng-model="contact.lastname" username-available ng-model-options="{ updateon: 'blur' }"> <div ng-if="contactform.$pending.usernameexists">checking....</div> <div ng-show="alreadyexist=='true'" class="alert alert-info" role="alert" style="margin-top:10px;"> <span class="glyphicon glyphicon-alert" aria-hidden="true"></span> <span class="sr-only">error:</span> last name exists </div> </div> </div> <div class="form-group"> <label for="txtfirstname" class="col-sm-2 control-label">first name</label> <div class="col-sm-10"> <input type="text" class="form-control" name="txtfirstname" maxlength="100" placeholder="enter first name" ng-model="contact.firstname"> </div> </div>
currently, script working. when page loaded data user correctly retrieved , displayed. script equally loaded , message "last name exists" appears. avoid , displays message when value of last name updated (and used).
could tell me how update script (of directive suppose) doing that?
thanks in advance help.
Comments
Post a Comment