AngularJS is not updated when $ scope. $ Apply is called

Here's the workflow:

On the user account page, the entire object belonging to this user must be indicated.

Next to each object is the “delete” button, which opens the Bootstrap modality. The modal asks the user if he really wants to delete the object, and if they confirm, then the modal must leave, the object must be deleted, and the view must be updated to reflect the deletion.

I reject the modal attribute data-dismisson the confirmation button inside the modal file.

Here is a function of my controller that deletes an object and (should) update the view:

 $scope.deleteObject = function(object) {
    object.destroy({
      success: function(object) {
        $scope.$apply();
      }, 
      error: function(object, error) {
        // handle error
      }
    });
  };

However, I need to refresh the page to view the updated view with the deleted object.

$scope.$apply?

EDIT: , $scope . ( - .

, :

.controller('AccountCtrl', function($scope) {
  var query = new Query('Object');
  query.find().then(function(objects) {
    $scope.objects = objects;
  });

  $scope.deleteObject = function(object) {
    object.destroy({
      success: function(object) {
        // do something
      }
    });
  }
});

find $scope, :

.controller('AccountCtrl', function($scope) {
  $scope.getObjects = function() {
    var query = new Query('Object');
    query.find().then(function(objects) {
      $scope.objects = objects;
    });
  }

  $scope.getObjects(); // call when the view loads

  $scope.deleteObject = function(object) {
    object.destroy({
      success: function(object) {
        $scope.getObjects(); // call again when an object is deleted
      }
    });
  }
});

, , , .

+4
1

$scope.object.

( , ):

$scope.deleteObject = function(object) {
    object.destroy({
      success: function(object) {
        var objectIndex = $scope.objects.indexOf(object);
        $scope.objects.splice(objectIndex,1);
      }
    });
  }

. Angular . $scope.getObjects() - . - . , .

, . , .

0

All Articles