Javascript 检查angular.js中的服务器上是否已存在数据

Javascript 检查angular.js中的服务器上是否已存在数据,javascript,angularjs,Javascript,Angularjs,我是angular.js的新手 <input type="email" disabled='disabled' name="email" ng-model="userInfo.emailAddress" class="input-xlarge settingitem" style="height:30px;"> <span ng-show="myForm.email.$error.email" cl

我是angular.js的新手

 <input type="email" disabled='disabled' name="email" ng-model="userInfo.emailAddress" 
                                    class="input-xlarge settingitem" style="height:30px;"> 

<span ng-show="myForm.email.$error.email" class="help-inline" style="color:red;font-size:10px;">
                                        Not a Valid Email Address</span>

不是有效的电子邮件地址
我有一个email字段,我需要在服务器上检查它是否已经存在于数据库中


是否有人可以指导我如何使用angular指令和服务在服务器上进行检查?我建议编写一个插入管道的指令(从中检查“自定义验证”)

以下是此类指令的示意图:

.directive('uniqueEmail', ["Users", function (Users) {
  return {
    require:'ngModel',
    restrict:'A',
    link:function (scope, el, attrs, ctrl) {

      //TODO: We need to check that the value is different to the original

      //using push() here to run it as the last parser, after we are sure that other validators were run
      ctrl.$parsers.push(function (viewValue) {

        if (viewValue) {
          Users.query({email:viewValue}, function (users) {
            if (users.length === 0) {
              ctrl.$setValidity('uniqueEmail', true);
            } else {
              ctrl.$setValidity('uniqueEmail', false);
            }
          });
          return viewValue;
        }
      });
    }
  };
}])
其中,
Users.query
是一个异步调用,用于检查电子邮件是否唯一。当然,您应该将此替换为对后端的调用

完成后,本指令可按如下方式使用:

<input type="email" ng-model="user.email" unique-email>


该指令的示例取自AngularJS社区的一些成员试图组合起来说明常见用例的示例。也许值得一试,看看所有这些在整个应用程序中是如何结合在一起的。

谢谢你的回答,你能不能建议如何比较之前的值。你的解决方案运行良好,但我面临一个问题,当我从服务器收到回叫时,我的整个表单变得清晰。你如何从服务器呼叫?