另一个功能完成后如何运行功能?

时间:2016-01-12 09:52:01

标签: javascript angularjs

我有一个Angular函数:

$scope.addMovie = function(movie){
  addMovieRecord(movie)
  addMovieCredits(movie)
}

两个Javascript函数:

var addMovieRecord = function (movie){

  createMovie.create({
    id:             $scope.movieListID.id,
    release_date:   releaseNL.release_date,
    imdb_rating:    $scope.movieImdbRating.imdbRating,
    title:          $scope.movieListID.original_title,
    image:          $scope.movieListID.poster_path,
    movie_id:       $scope.movieListID.id,
    backdrop:       $scope.movieListID.backdrop_path,
    overview:       $scope.movieCredits.overview
  })

}

var addMovieCredits = function (movie){

  movieService.loadMovies().then(function(response) {
  $scope.moviesCheck = response.data;

}

因此,当我运行$scope.addMovie函数时,它应首先触发创建电影记录的addMovieRecord函数。然后,createMovie应触发addMovieCredits以加载所有记录。

但这并没有发生。 addMovieCredits返回所有记录,但刚刚创建的记录除外。所以看起来订单不正确,

如果我在创建函数后直接触发addMovieCredits (movie),它会加载所有记录,

createMovie.create({
  id:             $scope.movieListID.id,
  release_date:   releaseNL.release_date,
  imdb_rating:    $scope.movieImdbRating.imdbRating,
  title:          $scope.movieListID.original_title,
  image:          $scope.movieListID.poster_path,
  movie_id:       $scope.movieListID.id,
  backdrop:       $scope.movieListID.backdrop_path,
  overview:       $scope.movieCredits.overview
}).then(addMovieCredits(movie));

但那不是我想要的。我想在添加功能完成后加载电影记录。

有关如何执行此操作的任何建议吗?

*更新*

这是createMovie服务,

app.factory('createMovie', ['$http', function($http){
  return{
    create: function(movie){
      return $http.post('/movies.json', movie);
    }
  };
}]);

3 个答案:

答案 0 :(得分:2)

考虑到createMovie是一个$resource对象,你可以这样做:

(我在您的代码中不确定createMovie$scope函数,而且您在{{1}上调用create应该有一些命名冲突)

createMovie

然后,使用返回的承诺

var addMovieRecord = function (movie){
  // return from here which should be a promise
  return createMovie.create({
    id:             $scope.movieListID.id,
    release_date:   releaseNL.release_date,
    imdb_rating:    $scope.movieImdbRating.imdbRating,
    title:          $scope.movieListID.original_title,
    image:          $scope.movieListID.poster_path,
    movie_id:       $scope.movieListID.id,
    backdrop:       $scope.movieListID.backdrop_path,
    overview:       $scope.movieCredits.overview
  });
}

<强>更新

您的问题中的语法$scope.addMovie = function(movie) { // or try "success" instead of "then" (may vary based on your Angular's version) addMovieRecord(movie).then(function() { addMovieCredits(movie) }); } 也应该有效,除非出现问题。 .then(loadMovies(movie));函数需要一个函数,但是在使用then语法中的()立即调用函数后,您将传递该函数。

答案 1 :(得分:1)

试试这个。

$scope.createMovie = function(movie){
  addMovie(movie).$promise.then(function(){
      loadMovies(movie)
  });
};

答案 2 :(得分:0)

movieService.loadMovies().then(function(response) {
$scope.moviesCheck = response.data;}

您在控制器的大小调整中将此服务称为一次 每次点击按钮,调用AddMovie函数传递电影列表时,你都会添加一部电影。

<button ng-click="AddMovie(moviesCheck)"></button>   


 $scope.addMovie = function (moviesCheck){

      var movie ={
        id:             $scope.movieListID.id,
        release_date:   releaseNL.release_date,
        imdb_rating:    $scope.movieImdbRating.imdbRating,
        title:          $scope.movieListID.original_title,
        image:          $scope.movieListID.poster_path,
        movie_id:       $scope.movieListID.id,
        backdrop:       $scope.movieListID.backdrop_path,
        overview:       $scope.movieCredits.overview
      }

    movieService.AddMovie(Movie) ; 

// you call the service who add the movie at database
then you add the movie at the existing list in the view

    moviesCheck.push(Movie); 

}