Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数组中删除项需要很多时间_Javascript_Angularjs - Fatal编程技术网

Javascript 从数组中删除项需要很多时间

Javascript 从数组中删除项需要很多时间,javascript,angularjs,Javascript,Angularjs,在我的控制器中我有两个功能,第一个允许我向数组添加新项,第二个删除单个项 我的整个控制器如下所示: .controller('MyCtrl', function($scope, $mdDialog) { $scope.questions = [{ text: 'What is Your name?', files: [{ id: 1, name: 'file 1' }] }, { text: 'Question 2', fi

在我的控制器中我有两个功能,第一个允许我向数组添加新项,第二个删除单个项

我的整个控制器如下所示:

.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);
  };
});
添加新项目很快,但删除需要更多时间:

以下是Plunker展示的问题:

我的问题是:如何加快删除分配给问题的文件的速度(我应该如何优化deleteFile函数)?

您可以在特定元素上执行此操作

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

请参见

@ArunPJohny我正在使用
ngClick
进行添加和删除。添加很快,但删除很慢。
ngClick
的延迟应该会影响两个按钮吗?@ArunPJohny,我接受的不止这些…很抱歉。。。延迟是从角度发生的。。。在调用
deleteFile
来呈现UI之后,它是由ngAnimate和css转换时间引起的调整css中的转换时间