【问题标题】:AngularJS $index works incorrectly after orderByAngularJS $index 在 orderBy 之后工作不正确
【发布时间】:2016-02-26 12:43:52
【问题描述】:

在用户可以对项目进行投票的项目上工作,并且在 orderBy 之后,该功能仅适用于项目的原始索引。我想通过从数据库中提取但有问题的 ID 属性来跟踪项目。我也尝试过传递对象而不是它们的索引,但我再次无法理解它。

相关JS

angular.module("cardSite", ['masonry'])
    .controller('mainCtrl', function($scope, dataService) {
        $scope.cards = [];
        dataService.getCards().then(function(response) {
            $scope.cards = response.data;
        }, function() {});

        $scope.plusOne = function($index) {
            $scope.cards[$index].card_rating += 1;
        };
    });

相关 HTML

<div class="col-lg-3 tile grid-item" ng-repeat="card in cards | orderBy : '-card_rating' | limitTo:8 track by card.ID">
<div class="panel panel-default {{ card.card_category | lowercase }}_panel">
    <p>Card ID: {{card.ID}}</p>
    <!DOCTYPE svg>
    <p ng-click="plusOne($index)"> Vote up</p>
    <p>{{card.card_rating}}</p>
</div>

有人有什么建议吗?

【问题讨论】:

标签: javascript angularjs


【解决方案1】:

在这种情况下为什么需要索引?

你可以直接传递对象:

<p ng-click="plusOne(card)"> Vote up</p>

并将您的功能更改为:

$scope.plusOne = function(card) {
        card.card_rating += 1;
};

【讨论】:

  • 我似乎把问题复杂化了,这正是我所需要的,谢谢!
  • 不!容易想太多!如果它解决了您的问题,请随时接受答案:)
  • 只是在等待 10 分钟用完!再次感谢
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
  • 2013-04-07
  • 2013-06-13
相关资源
最近更新 更多