2014-04-28 84 views
1

大多數情況下,此AngularJS應用程序中的刪除功能(刪除記錄並顯示成功消息),但視圖不更新。

我試過添加$scope.$apply()但這隻給我一個$digest already in progress錯誤。但是,如果摘要正在進行中,不應該更新視圖嗎?這不是什麼摘要呢?

視圖

<button ng-click="deleteNote(note)">Delete</button> 

控制器

LessonNotes.controller("NotesCtrl", ["$scope", "$routeParams", "$location", "Note", "NoteType", "Alert", 
    function($scope, $routeParams, $location, Note, NoteType, Alert) { 

    $scope.notes = Note.query(); 

    $scope.deleteNote = function(note) { 
     if(confirm("Are you sure you want to delete this note?")) { 
     note.$destroy(
      function(n, responseHeaders) { 
      $scope.$apply(); 
      Alert.add("success", "Note deleted successfully!", 5000); 
      }, 
      function() { 
      Alert.add("warning", "There was an error deleting the note!", 5000); 
      } 
     ); 
     } 
    }; 

}]); 

服務

LessonNotes.factory("Note", ["$resource", function($resource) { 
    return $resource("lesson_notes/notes/:id", { id: "@id" }, { 
    query: { method: 'GET', params: {}, isArray: true }, 
    update: { method: 'PATCH' }, 
    destroy: { method: 'DELETE' } 
    }); 
}]); 

在CAS重要的是,我使用Rails作爲後端。

+1

第二個答案可能會幫助: http://stackoverflow.com/questions/12729122/prevent-error-digest-already-in-progress-when-calling-scope-apply – ryanlutgen

回答

1

爲防止$digest already in progress錯誤,請嘗試在Angular的$timeout函數中運行您的代碼。

此外,您沒有使用$ scope。$正確應用,您想要像這樣在代碼中包裝$ apply調用。

$timeout(function() { 
    $scope.$apply(function() { 
     note.$destroy... 
    }); 
}); 
這裏
相關問題