Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么angularjs中的$AsyncValidator返回一个承诺,但没有定义_Javascript_Angularjs - Fatal编程技术网

Javascript 为什么angularjs中的$AsyncValidator返回一个承诺,但没有定义

Javascript 为什么angularjs中的$AsyncValidator返回一个承诺,但没有定义,javascript,angularjs,Javascript,Angularjs,所以我对整个web都是新手,今天我发现了$AsyncValidator 因此,经过多次尝试,我被这个例子困住了 用户名验证程序 (function() { angular .module('app') .directive('emailNotUsed',emailNotUsed); emailNotUsed.$inject = ['$http', '$q']; function emailNotUsed ($http, $q) {

所以我对整个web都是新手,今天我发现了$AsyncValidator

因此,经过多次尝试,我被这个例子困住了

用户名验证程序

(function() {

    angular
        .module('app')
        .directive('emailNotUsed',emailNotUsed);

    emailNotUsed.$inject = ['$http', '$q'];

    function emailNotUsed ($http, $q) {
        return {

            require: 'ngModel',
            link: function(scope, element, attrs, ngModel) {
                ngModel.$asyncValidators.emailNotUsed = function(modelValue, viewValue) {


                    console.log("start");
                    console.log(viewValue);
                   console.log($http.post('/email', viewValue));


                  $http.post('/email',viewValue).then(function(response) {
                        console.log("Check if email is valid in directive")
                        console.log(response.data)
                        return response.data == true ? $q.reject(response.data.errorMessage) : true;
                    });
                };
            }
        };
    }
}());
register.html

<div class="container" ng-controller="RegisterController as vm">
    <div class="col-md-6 col-md-offset-3">
        <h2>Register</h2>
        <div ng-show="vm.error" class="alert alert-danger">{{vm.error}}</div>
        <form name="form" ng-submit="!form.$pending && form.$valid && vm.register()" role="form">

            <div class="form-group" ng-class="{ 'has-error': form.email.$dirty && form.email.$error.required }">
                <label for="email">Email</label>
                <input type="text" name="email" id="email" class="form-control" ng-model="vm.user.email" email-not-used ng-model-options="{ debounce: 500 }" required />

                <div ng-messages="form.email.$error">
                    <div ng-message="emailNotUsed">User with this email already exists.</div>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Register</button>
                <a href="#/login" class="btn btn-link">Cancel</a>
            </div>
        </form>
    </div>
</div>

登记
{{vm.error}
电子邮件
具有此电子邮件的用户已存在。
登记
现在它变得奇怪的地方是控制台中的输出


那么,当我得到承诺值时,为什么会出现错误呢?任何关于如何继续的想法,异步验证程序都希望返回承诺,因此必须修改验证函数以返回
$http
调用的结果,如下所示:

ngModel.$asyncValidators.emailNotUsed = function(modelValue, viewValue) {
    return $http.post('/email',viewValue).then(function(response) {
        return response.data == true ? $q.reject(response.data.errorMessage) : true;
    });
};

它希望返回承诺,因此:
$http.post('/email',viewValue)
应该是
返回$http.post('/email',viewValue)
。我发现我之前在不同的问题中发布的示例中出现了这个错误。@BohuslavBurghardt您最好将其作为答案发布,这是正确的。再次感谢,现在我得到了一些工作示例来做一些练习。