仅在AngularJS中$save成功后重定向



我正在张贴一个表单,我想重定向到我的列表页面,以防表单没有错误/成功持久化/保存到数据库。我该如何做到这一点?

app.controller('NoteCreateController',
  ['$scope', 'Note', '$routeParams', '$location','ShareNoteScope',
    function($scope, Note, $routeParams, $location, ShareNoteScope) {
    $scope.notes = ShareNoteScope.getScope().notes;
    $scope.newNote = {};
    $scope.createNote = function(note) {
      var newNote = new Note(note);
      newNote.$save(function(newNote) {
        $scope.notes.unshift(newNote.note);
        $scope.note = '';
        $scope.errors = '';
      }, function(newNote) {
        $scope.errors = newNote.data;
        // $location.path('/notes/'+newNote.note.id); where do I put this?
      });
    }
}]);

$save用回调封装成功调用。上面的这段代码应该可以完成任务。

newNote.$save(function (newNote) {
    //success callback
    $location.path('/notes/' + newNote.note.id);
}, function (newNote) {
    $scope.errors = newNote.data;
});

最新更新