Misiu
Misiu

Reputation: 4919

deleting item from array takes much time

Inside my controller I have two functions, first allows me to add new item to array, second removes single item.

My whole controller looks like this:

.controller('MyCtrl', function($scope, $mdDialog) {
  $scope.questions = [{
    text: 'What is Your name?',
    files: [{
      id: 1,
      name: 'file 1'
    }]
  }, {
    text: 'Question 2',
    files: [{
      id: 1,
      name: 'file 1'
    }]
  }, {
    text: 'Question 3',
    files: [{
      id: 1,
      name: 'file 1'
    }]
  }];

  $scope.selectedQuestionIndex = undefined;
  $scope.selectedQuestion = function(index) {
    if ($scope.selectedQuestionIndex !== index) {
      $scope.selectedQuestionIndex = index;
    } else {
      $scope.selectedQuestionIndex = undefined;
    }
  };

  $scope.addFile = function(question) {
    question.files.push({
      id: 2,
      name: 'file 2'
    });
  };
  $scope.deleteFile = function(question, file) {
    var index = question.files.indexOf(file);
    question.files.splice(index, 1);
  };
});

Adding new items is fast, but deleting takes much more time: delete delay

Here is Plunker showing issue: http://plnkr.co/edit/YYxeHlubDk0VThmQNmw4?p=preview

My question is: How can I speed up removing files assigned to question (how should I optimize deleteFile function)?

Upvotes: 2

Views: 65

Answers (1)

karaxuna
karaxuna

Reputation: 26940

You can disable animation on specific element.

.directive('noAnimate', ['$animate', function (animate) {
    return function (scope, element) {
        animate.enabled(element, false); 
    };
}])

See plunker

Upvotes: 2

Related Questions