I have my registration form with the username in the text box. I want to do when the user enters the username, the user directive checks if the entered username 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 exists, my div class = "invalid" will be displayed in html form labeled "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 right? Please advise or suggest me what I should do. Thanks in advance.
source share