【问题标题】:deleting item from array takes much time从数组中删除项目需要很多时间
【发布时间】:2016-02-17 10:58:48
【问题描述】:

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

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

.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 显示问题:http://plnkr.co/edit/YYxeHlubDk0VThmQNmw4?p=preview

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

【问题讨论】:

  • @ArunPJohny 我使用ngClick 进行添加和删除。添加很快,但删除很慢。 ngClick 的延迟应该会影响两个按钮?
  • @ArunPJohny,我需要的不止这些……
  • 对此感到抱歉...延迟发生在角度方面...在调用deleteFile 来呈现 UI 之后
  • 这是由ngAnimate和css过渡时间引起的
  • 调整css中的过渡时间

标签: javascript angularjs


【解决方案1】:

您可以在特定元素上disable animation

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

plunker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2020-01-05
    • 2011-02-07
    • 2014-01-03
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多