I have my registration form with textbox username. I want to do is when the user enter the username, the custom directive will check if the entered username is exists in the database.
directives.js
angular.module('installApp').directive('pwCheck', function ($http) { return { require: 'ngModel', link: function (scope, elem, attrs, ctrl) { elem.on('blur', function (evt) { scope.$apply(function () { $http({ method: 'GET', url: '../api/v1/users', data: { username:elem.val(), dbField:attrs.ngUnique } }).success(function(data, status, headers, config) { ctrl.$setValidity('unique', data.status); }); }); }); } } });
If it's exists, my div class = "invalid"
will shown in the html form with label "Username already exists."
registration.html
<form name = "signupform"> <label>{{label.username}}</label> <input type="text" id = "username" name = "username" ng-model="user.username" class="form-control"></input> <div class="invalid" ng-show="signupform.username.$dirty && signupform.username.$invalid"><span ng-show="signupform.username.$error.unique">Username already exists.</span> </div> </form>
But right now, they are not working :-(,am I doing right? Please advice or suggest me things I should do. Thanks in advance.