【问题标题】:Angular - trouble with splicing an arrayAngular - 拼接数组的麻烦
【发布时间】:2014-05-25 21:59:48
【问题描述】:

我对 AngularJS 很陌生,所以我确定我只是犯了一个简单的错误。

我想在var newCard = cardTypes.shift(); 行上拼接cardTypes 数组,而不是使用.shift(),这样我就可以考虑我的ng-repeat 索引。

.shift() 可以正常工作,但如果我尝试.splice(index, 1),则根本没有数据。

任何帮助将不胜感激。

    .controller('CardsCtrl', function($scope, $ionicSwipeCardDelegate) {
  var cardTypes = [
    { title: 'Swipe down to clear the card', image: 'img/pic.png' },
    { title: 'Where is this?', image: 'img/pic.png' },
    { title: 'What kind of grass is this?', image: 'img/pic2.png' },
    { title: 'What beach is this?', image: 'img/pic3.png' },
    { title: 'What kind of clouds are these?', image: 'img/pic4.png' }
  ];

  $scope.cards = Array.prototype.slice.call(cardTypes, 0, 0);

  $scope.cardSwiped = function(index) {
    $scope.addCard();
  };

  $scope.cardDestroyed = function(index) {
    $scope.cards.splice(index, 1);
      $scope.addCard();
  };

  $scope.addCard = function() {
    var newCard = cardTypes.shift();
    newCard.id = Math.random();
    $scope.cards.push(angular.extend({}, newCard));
  }
})

.controller('CardCtrl', function($scope, $ionicSwipeCardDelegate) {
  $scope.goAway = function() {
    var card = $ionicSwipeCardDelegate.getSwipebleCard($scope);
    card.swipe();
  };
});

【问题讨论】:

  • 我看不出这与 Angular 有什么关系。

标签: javascript arrays angularjs ionic-framework


【解决方案1】:

你说的是addCard函数吗?

该函数中没有index 变量,所以也许您打算使用.splice(0, 1); 删除第一个变量?

'use strict'; 添加到我所有JavaScript 文件的顶部有助于我发现类似的问题。

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 2017-01-20
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多