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作爲後端。
第二個答案可能會幫助: http://stackoverflow.com/questions/12729122/prevent-error-digest-already-in-progress-when-calling-scope-apply – ryanlutgen