Javascript 如何使用按钮添加+;1到使用AngularJS的字段?
我正在努力学习AngularJS。对于我的第一个项目,我想创建一个小型记分应用程序。我从一个JSON文件中获取数据,然后我想根据谁的分数在屏幕上更新它 我似乎不知道如何使用Angular更新特定的目标区域。我当前的应用程序将新行推送到记分表:Javascript 如何使用按钮添加+;1到使用AngularJS的字段?,javascript,angularjs,Javascript,Angularjs,我正在努力学习AngularJS。对于我的第一个项目,我想创建一个小型记分应用程序。我从一个JSON文件中获取数据,然后我想根据谁的分数在屏幕上更新它 我似乎不知道如何使用Angular更新特定的目标区域。我当前的应用程序将新行推送到记分表: 'use strict'; /* Controllers */ function ScoreKeeper($scope, $http) { $http.get('json/hometeam.json').success(function(data
'use strict';
/* Controllers */
function ScoreKeeper($scope, $http) {
$http.get('json/hometeam.json').success(function(data) {
$scope.hometeam = data;
});
$scope.addGoal = function() {
$scope.hometeam.push({goals: +1});
};
}
这是我当前使用的表:
<table class="table table-hover">
<thead>
<th>Player ID</th>
<th>No.</th>
<th>Name</th>
<th>GP</th>
<th>G</th>
<th>A</th>
<th>PIM</th>
<th>Goal</th>
<th>Assist</th>
<th>Assist</th>
</thead>
<tbody>
<tr ng-repeat="player in hometeam" >
<td>{{player.playerid}}</td>
<td>{{player.no}}</td>
<td>{{player.name}}</td>
<td>{{player.gp}}</td>
<td>{{player.goals}}</td>
<td>{{player.assists}}</td>
<td>{{player.pim}}</td>
<td><button class="btn btn-small" name="add-goal" type="button" ng-click="addGoal()"><i class="icon-plus"></i></button></td>
</tr>
</tbody>
</table>
玩家ID
不
名称
全科医生
G
A.
PIM
目标
帮助
帮助
{{player.playerid}
{{player.no}
{{player.name}
{{player.gp}
{{player.goals}}
{{球员助攻}
{{player.pim}
我希望能够为特定玩家单击addGoal,然后更新他们的目标总数。现在,当前函数正在创建一个目标为1的新行,而所有其他字段都为空
我对这一点很迷茫,数据在刷新时正确呈现,但是,我的更新函数不正确。我知道.push()不是正确的方法,但是我似乎找不到一个好的资源来帮助Angular找出正确的方法。非常感谢您的帮助。谢谢。您需要增加关联玩家物品的goals属性,而不是使用push创建新的阵列物品
ng单击=“添加目标(玩家)”
或者只是
ng click=“player.goals=player.goals+1”
我猜您想在player.goals
中添加元素,对吗
然后你可以做:
<button ng-click="addGoal($index)">
非常好用,非常感谢。现在来看,这很有意义。
<button ng-click="addGoal($index)">
$scope.addGoal = function(index) { $scope.hometeam[index].goals++; };