【问题标题】:issue with custom directive for checking unique email id用于检查唯一电子邮件 ID 的自定义指令问题
【发布时间】:2016-10-06 23:40:08
【问题描述】:

我正在处理 Angularjs 表单,我需要验证输入值是否在数据库中。 我的html如下

<div class="form-group mg-btm0" show-errors>
                <label class="control-label col-sm-4 fw-norm txt-right">Email ID:</label>
                <div class="col-sm-8">
                    <input type="email" class="form-control" placeholder="Email ID" name="emailAddress" ng-required="true" autocomplete="Off" ng-model="User.emailId"
                        ng-maxlength="100" emailid-available-validator>
                    <span class="help-block" ng-if="RegisterUser.emailAddress.$error.email">A valid email is required.</span>
                    <span class="help-block" ng-if="RegisterUser.emailAddress.$error.maxlength">Max length is 100.</span>
                    <span class="help-block" ng-if="RegisterUser.emailAddress.$pending"> Checking emailId...</span>
                    <span class="help-block" ng-if="RegisterUser.emailAddress.emailidAvailable">EmailId entered has already been registered</span>
                </div>
            </div>

指令如下

angularFormsApp.directive('emailidAvailableValidator', ['$http', function ($http) {
    var baseurl = window.location.protocol + "//" + window.location.host + "/";
    return {
        require: 'ngModel',
        link: function ($scope, element, attrs, ngModel) {
            ngModel.$asyncValidators.emailidAvailable = function (value) {
                //var emailid = { 'emailid': emailaddress };
                return $http.get(baseurl + "Users/CheckExternalUserExists/", { emailid: value });
            };
        }
    }
}])

控制器如下

[HttpGet]
        public ActionResult CheckExternalUserExists(string emailid)
        {
            if (AccountExecutor.UserExists(emailid))
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Exists");
            }
            return new HttpStatusCodeResult(HttpStatusCode.OK, "Not Exists");
        }

现在我遇到的问题是。

1 .电子邮件 ID 作为 null 传递给控制器​​。

2 。我想显示两条消息

a) 如果 email id 是唯一的,那么消息应该说明它的唯一性

b) 如果电子邮件 id 存在,那么它应该显示它存在的消息, 当前消息未显示,仅显示红色边框。

【问题讨论】:

    标签: angularjs asp.net-mvc angularjs-directive


    【解决方案1】:
     try below in your code
    
      ngModel.$asyncValidators.emailidAvailable = function (ngModel.$modelValue)  {
                //var emailid = { 'emailid': emailaddress };
                return $http.get(baseurl + "Users/CheckExternalUserExists/", { emailid: value });
            };
    
      check out below link for more information...
        [asyncValidators][1]
    

    【讨论】:

    • 它有什么不同??
    猜你喜欢
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2014-07-19
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多