title: Angular directive for unique username
body: <p>I looked at a bunch of angularjs directives checking for uniqueness of a username and decided to try for the simplest implementation. It does what I want but may not really be 'idiomatic' angular.  Here is the form element:</p>
  
  <pre><code>&lt;input type="text"
      name="username"
      ng-model="form.username"
      unique-username=""
      required
  /&gt;
  &lt;span class="hide-while-in-focus" ng-show="thisform.username.$error.unique"&gt;Username taken!&lt;/span&gt;
  </code></pre>
  
  <p>and here's the directive:</p>
  
  <pre><code>.directive('uniqueUsername', function($http) {
        return {
             restrict: 'A',
             require: 'ngModel',
             link: function (scope, element, attrs, ngModel) {
                  element.bind('blur', function (e) {
                       ngModel.$setValidity('unique', true);
  
                       $http.get("/api/checkUnique/" + element.val()).success(function(data) {
                            if (data) {
                                ngModel.$setValidity('unique', false);
                            }
                       });
                  });
             }
        };
  })
  </code></pre>
  
  <p>And the expressjs call</p>
  
  <pre><code>if (data) {
        console.log("found " + data.username);
        return res.send(data.username);
  }
  else {
        console.log("not found");
        return res.send(404);
  }
  </code></pre>
  
  <p>I would appreciate any feedback on why this is good or bad and if possible a revision that uses $scope.watch on the model. </p>