Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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 在Angular JS中同步回调_Javascript_Angularjs_Callback - Fatal编程技术网

Javascript 在Angular JS中同步回调

Javascript 在Angular JS中同步回调,javascript,angularjs,callback,Javascript,Angularjs,Callback,我正在尝试同步两个回调,一个来自服务,另一个直接调用。(这是旧代码,请忽略代码质量) 服务: app.factory('UserValidateService', function($http){ var findUserByEmail = function (user, email, callback) { $http.get("/api/user/"+user+"/email/"+email) .success(callback); };

我正在尝试同步两个回调,一个来自服务,另一个直接调用。(这是旧代码,请忽略代码质量)

服务:

app.factory('UserValidateService', function($http){

    var findUserByEmail = function (user, email, callback) {
        $http.get("/api/user/"+user+"/email/"+email)
        .success(callback);
    };

    return {
        findUserByEmail: findUserByEmail
    };
});
错误:

TypeError: Cannot read property 'then' of undefined
    at h.$scope.register (user.js:118)
    at angular.js:10567
    at angular.js:18627
    at h.$eval (angular.js:12412)
    at h.$apply (angular.js:12510)
    at HTMLButtonElement.<anonymous> (angular.js:18626)
    at HTMLButtonElement.jQuery.event.dispatch (jquery.js:5095)
    at HTMLButtonElement.elemData.handle (jquery.js:4766)
TypeError:无法读取未定义的属性“then”
位于h.$scope.register(user.js:118)
在angular.js:10567
在angular.js:18627
高度为$eval时(角度:12412)
在高$apply时(angular.js:12510)
在HTMLButtoneElement。(angular.js:18626)
在HTMLButtonElement.jQuery.event.dispatch(jQuery.js:5095)
位于HTMLButtonElement.elemData.handle(jquery.js:4766)

请告诉我哪里出错,或者如何使此代码同步,即调用您的
UserValidateService
不返回承诺,它使用
$http
成功方法的回调。这是一个应该避免的问题

将您的工厂转换为从
$http
服务返回承诺:

app.factory('UserValidateService', function($http){

    var findUserByEmail = function (user, email) {
        return $http.get("/api/user/"+user+"/email/"+email)
    };

    return {
        findUserByEmail: findUserByEmail
    };
});
然后使用promise的
.Then
方法执行回调功能

var promise = UserValidateService
                  .findUserByEmail($scope.newUser.username,
                                   $scope.newUser.email);

var derivedPromise = promise.then ( function(response){
    console.log(response.user);
    if(response.user[0]) {
        alert('email Id already exists ******');
    });
    //return response for chaining
    return response;
});

后续操作可能会链接出
derivedPromise

UserValidateService.FindUserByMail()是否返回承诺?这是一个回调函数:使用服务代码编辑问题。如果您尝试同步,我建议使用$q延迟。创建两个承诺,然后与$q.all同步。我发现有人制作了一个JFIDLE,可以作为一个例子:为了修复错误,您需要从服务中返回承诺。除此之外,我仍然不明白“同步两个回调”是什么意思
var promise = UserValidateService
                  .findUserByEmail($scope.newUser.username,
                                   $scope.newUser.email);

var derivedPromise = promise.then ( function(response){
    console.log(response.user);
    if(response.user[0]) {
        alert('email Id already exists ******');
    });
    //return response for chaining
    return response;
});