Marcus Mullen
Marcus Mullen

Reputation: 13

angularjs - Splice removing all objects from scope array instead of specific object

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?

Upvotes: 0

Views: 1178

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

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);

Upvotes: 1

Related Questions