然后在Angular完成承诺后没有回应

时间:2016-08-15 12:13:51

标签: angularjs

我有控制器,我在另一个控制器中广播一个事件:

getmovieController.js:

$scope.removeMovie = function(movie){
    $rootScope.$broadcast('onremoveMovieEvent', movie);
};

这是另一个控制器,我在其中调用一个请求工厂内部函数的函数

moviesearchController.js:

$scope.$on('onremoveMovieEvent', function (event, movie) {
    movieFactory.removeMovie(movie).then(function(){
        Notification.success(movie.title + ' has been removed from your watchlist')
    })
});

这是具有removeMovie功能的工厂。

movieFactory.js

var factory = {}

factory.removeMovie = function (movie) {
    var deferred = $q.defer();
    $http({
        method: 'DELETE',
        url: '/movies',
        data: movie,
        headers: {"Content-Type": "application/json;charset=utf-8"}
    })
        .success(function(){
            console.log('success factory');
        })
        .catch(function(){
        });
    return deferred.promise;
};

return factory;

最后我有一个路由文件响应删除

movies.js

router.delete('/', function(req,res){

    pool.getConnection(function(err, connection){

        connection.query('DELETE FROM movies WHERE id= ?', [req.body.id], function(err, result) {
            if (err) {
                throw err;
            } else {
                console.log('removed')
            }
        });
        connection.release();
    });
    res.status(204).end();
});

这就是所有相关的代码。现在问题。当我在第一个控制器中调用$scope.removeMovie函数时,单击的记录将从数据库中删除,工厂将在控制台中显示success factory日志。但我无法从第二个控制器进入.then回调

$scope.$on('onremoveMovieEvent', function (event, movie) {
    movieFactory.removeMovie(movie).then(function(){
        Notification.success(movie.title + ' has been removed from your watchlist')
    })
});

为什么在成功的工厂功能之后我无法进入.then回调?

3 个答案:

答案 0 :(得分:1)

我认为工厂内成功回调中缺少 deferred.resolve()执行:

factory.removeMovie = function (movie) {
    var deferred = $q.defer();
    $http({
        method: 'DELETE',
        url: '/movies',
        data: movie,
        headers: {"Content-Type": "application/json;charset=utf-8"}
    })
        .success(function(response){
            console.log('success factory');
            deferred.resolve(response);
        })
        .catch(function(response) {
            deferred.reject(response);
        });
    return deferred.promise;
};

return factory;

您还应该使用以下方法处理 catch()方法: deferred.reject()

答案 1 :(得分:0)

您应该在工厂中包含deferred.resolvedeferred.reject功能。所以基本上你这样做:

factory.removeMovie = function (movie) {
  var deferred = $q.defer();
  $http({
    method: 'DELETE',
    url: '/movies',
    data: movie,
    headers: {"Content-Type": "application/json;charset=utf-8"}
  })
  .success(function(response){
    console.log('success factory');
    deferred.resolve(response); // Include response if you want
  })
  .catch(function(error){
    deferred.reject(error); // Include error if you want
  });

  return deferred.promise;
};

这实际上会设置成功/失败的承诺并返回响应。然后,控制器中的then功能应该可以正常工作。

答案 2 :(得分:0)

这是因为您没有解决或拒绝承诺。更新您的movieFactory.js

 var factory = {}

 factory.removeMovie = function (movie) {
    var deferred = $q.defer();
    $http({
        method: 'DELETE',
        url: '/movies',
        data: movie,
        headers: {"Content-Type": "application/json;charset=utf-8"}
   })
    .success(function(){
        console.log('success factory');
        deferred.resolve(/* Do send some response if necessary */);
    })
    .catch(function(){
        deferred.reject(/* Do send some response if necessary */);
    });
    return deferred.promise;
};

return factory;

如果您没有进行任何其他处理,您也可以直接返回$http服务返回的承诺。

 var factory = {}

 factory.removeMovie = function (movie) {
    return $http({
        method: 'DELETE',
        url: '/movies',
        data: movie,
        headers: {"Content-Type": "application/json;charset=utf-8"}
   });
};

return factory;