2016-01-22 30 views
0

Im尝试向ng-repeat创建的列表中的元素添加upvote按钮时出现问题。该清单是交易中的交易。该部分使用deal.controller和data-ng-init =“find()”。Angular - 在ng-repeat内添加upvote按钮

的deal.controller有funtion

// Find a list of Deals 

    $scope.find = function() { 

     $scope.deals = Deals.query(); 

    }; 

每个元素上的按钮使用NG单击

<button ng-click="upVote(deal._id)" type="button" class="btn btn-danger 
vote-up-button"><i class="glyphicon glyphicon-arrow-up"></i> Hot</button> 

它调用给予好评的功能

// upVoteDeal 

    $scope.upVote = function (id) { 

     $scope.deal.votes = $scope.deal.votes+1; 

     var deal = $scope.deal; 

     deal.$update(function() { 
      $location.path('deals/' + deal._id); 
     }, function(errorResponse) { 
      $scope.error = errorResponse.data.message; 
     }); 


    }; 

的问题,我有是不是说$scope.deal.votes = $scope.deal.votes+1;是错的。

及其与TypeError: Cannot read property 'votes' of undefined

+0

我认为你必须定义'了'$ scope.upvote'功能之外$ scope.deal.votes'像这样:'$ scope.deal = {votes:''}' –

+1

我可能是错的,但这似乎是一个全新的交易,而不是使用列表中的交易。 – ChrisM

回答

3

通行证回来在整个对象,而不是它的ID。

<button ng-click="upVote(deal)"> 

现在,你有你需要正确的功能在里面的一切控制器

$scope.upVote = function(deal) { 

    deal.votes++; 
    deal.$update(function() { 
     $location.path('deals/' + deal._id); 
    }, function(errorResponse) { 
     // rollback votes on fail also 
     $scope.error = errorResponse.data.message; 
    }); 
} 
+0

几乎发现,但似乎刷新和说禁止。有些不对吗?编辑 - 等待1秒。我认为它的数据库。 – ChrisM

+0

美丽谢谢。禁止我的Mongodb实例被停止。这真的是一个巨大的帮助。我只是稍微改变它,不重定向到视图页面。 – ChrisM