0

I'm trying to create an action that deletes a oject from the database and the object array it belongs to, but for some reason, it deletes all of the scope objects of that particular array instead of the one it's supposed to. Below is the system I have.

fact.add_delete = function(scope, func, db, hasCollection, collection, redirect) {
  scope.finish = null;
  scope[func] = function(id) {
    var query = 'DELETE FROM `' + db + '` WHERE id=' + id + '';
    var obj = scope[db].filter(function(value) {
      return (value.id == id)
    });
    $http.post('php/adb/update.php', {
      'query': query
    }).success(function(data) {
      console.log('Deleted from [' + db + '] at id of ' + id + '');
      scope[db].splice(obj);
      if (hasCollection) {
        scope[collection].splice(obj);
      }
      fact.load(scope);
      scope._finish = true;
    });
  };
};

it works for objects within an ngRepeat but not for parent-like objects. Any reason as to why?

1 Answer 1

1

The Structure of Array.prototype.splice() is

array.splice(start, deleteCount[, item1[, item2[, ...]]])

If you wanna delete sepecific item you have to provide index

Try like this

var index = scope[db].map(function(value){ return value.id; }).indexOf(id);
scope[db].splice(index,1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.