整理承诺响应后我有控制器,其中我广播在不同的控制器的事件:。然后在不角
getmovieController.js:
$scope.removeMovie = function(movie){
$rootScope.$broadcast('onremoveMovieEvent', movie);
};
这是其他控制器在我称之为
$scope.$on('onremoveMovieEvent', function (event, movie) {
movieFactory.removeMovie(movie).then(function(){
Notification.success(movie.title + ' has been removed from your watchlist')
})
});
:请求的函数工厂内
moviesearchController.js功能210
这是带有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
日志。但我不能从第二控制器
$scope.$on('onremoveMovieEvent', function (event, movie) {
movieFactory.removeMovie(movie).then(function(){
Notification.success(movie.title + ' has been removed from your watchlist')
})
});
为什么我不能进入一个全成工厂函数后.then
回调获取到.then
回调?
啊geez我怎么错过了这个。呃谢谢。 –
没问题......而且你知道它是如何的......有时候你只是在编码时盲目地看到这样的东西:P – thepio
是的,我应该只是喝了一些咖啡。感谢您指出它。我决定选择最低的代表为正确的答案,原因:)。 –