2016-03-31 109 views
0

目前我正在尝试使用angularJS保存更新的函数。直到现在我可以编辑数据,数据可以在数据库端更新,但是它并没有显示在前端。除非我必须注销并再次登录才能查看更新后的结果。我可以知道如何解决这个错误。AngularJS:无法保存数据

这是我的controller.js代码:

.controller('FilmDetailController', //havent done yet 
      [ 
       '$scope', 
       'dataService', 
       '$routeParams', 
       '$location', 
       '$window', 
       'UserInfo', 

       function ($scope, dataService, $routeParams, $location,$window, UserInfo){ 
        //var userName=dataService.getSessionService('user'); 
        if(UserInfo.loggedIn){ 
         $scope.film = [ ]; 
         $scope.filmCount = 0; 

         var getFilmDetail = function (moviecode) { 
          dataService.getFilmDetail(moviecode).then(
           function (response) { 
            $scope.film = response.data.ResultSet.Result; 
            //$scope.userLoginEmail = dataService.getSessionService('userEmail'); 
            $scope.showSuccessMessage = true; 
            $scope.successMessage = "Film Success"; 
           }, 
           function (err){ 
            $scope.status = 'Unable to load data ' + err; 
           } 
          ); // end of getStudents().then 
         }; 

         $scope.editedFilm = {}; 
         $scope.save_note = function ($event,film) { 
          $scope.editedFilm = film; 
          dataService.saveNote($scope).then(
           function (response) { 
            // getFilmDetail(); 
            $window.location.href = '#/movieList'; 
            //$window.location.reload(); 
            console.log("done"); 
           }, 

           function (err){ 
            $scope.status = 'Unable to load data ' + err; 
           } 
          ); 
          // $scope.reloadRoute = function() { 
          //  $location.path('/movieList'); 
          //  $window.location.reload() 
          // }//end of reload route fnction 

         } 

         // only if there has been a courseid passed in do we bother trying to get the students 
         if ($routeParams && $routeParams.filmid) { 
         // console.log($routeParams.filmid); 
          getFilmDetail($routeParams.filmid); 
         } 
        }else{ 
         $location.path('/login'); 
        } 

       } 
      ] 
     ); 

一旦我上保存笔记按钮的说明应在两个角侧进行更新,并在数据库端点击。目前它只能在数据库方面更新,除了角度方面。先谢谢您的帮助。

+0

是U使用$ scope.film显示在前端的数据。 – panwar

+0

是的,我使用$ scope.film来显示我的数据 – anonymous5671

+0

然后你应该在更新响应之后调用getFilmDeails(),即在console.log(“done”)之后,或者将$ scope.editedFilm赋值给$ scope.Film – panwar

回答

0

从我可以告诉它看起来您需要将笔记数据存储在服务中,并且当笔记已成功保存在数据库中时,您需要将新笔记推送到您的服务数据中。

我创建了一个简单的小提琴:https://jsfiddle.net/robrothedev/vgbqyv10/

function ItemService() { 
    var service = { 
    items: [], 
    addItem: addItem 
}; 

return service; 

function addItem(new_item) { 
    // run your http request and if the response is valid, 
    // push the new item to your service array 
    // $http.post(url,new_item).then(function(response) { 
    //  service.items.push(response.data.new_item); 
    // }); 
} 

}

function ItemsCtrl(ItemService) { 
    var vm = this; 

    vm.items = ItemService.items; 
    vm.new_item = {}; 
    vm.addItem = addItem; 

    function addItem() { 
    ItemService.addItem(vm.new_item); 
    vm.new_item = {}; 
    } 
}