如何在单击ng-repeat中的项时增加计数器
How do I increment a counter when clicking an item in an ng-repeat?
我用ng-repeat生成了一个列表,其中每个项目都有一个计数变量。在每个列表项中我都有一个链接。
我想在点击链接时增加计数
我试了下面的方法,但它不工作。
My Controller:-
myApp.controller('allpost', function ($scope, $http, $stateParams, Allposts) {
var id = $stateParams.id;
Allposts.GetAllposts(id).then(
function (response) {
$scope.allPosts = response.data.posts;
});
function ctrl($scope) {
$scope.increment = function(item){
item.count += 1;
}
}
})
和视图:-
<ion-content class="padding" lazy-scroll>
<div class="row no-padding HomeRowsList">
<div class="item itemfull" ng-repeat="post in allPosts">
<div class="item item-body">
<div>
<div class="title-news">
<div class="title" ng-bind-html="post.content"></div>
<div class="countbg">عدد المرات : {{post.custom_fields.azkarno}}</div>
<span>{{post.count}}</span><a onclick="ctrl(post);">Increment</a>
</div>
</div>
</div>
</div>
</div>
</ion-content>
使用
$scope.increment = function(item){
item.count += 1;
};
代替
function ctrl($scope) {
$scope.increment = function(item){
item.count += 1;
}
}
,在HTML中使用
<span>{{post.count}}</span><a data-ng-click="increment(post)">Increment</a>
代替
<span>{{post.count}}</span><a onclick="ctrl(post);">Increment</a>
应该是ng-click
而不是onclick
&方法名称应改为increment
,而不是ctrl
。
也从increment
方法中删除不必要的ctrl
函数包装器,这根本不需要,因为无论你想从html调用什么,都应该包含在控制器的$scope
中。
标记
<span>{{post.count}}</span><a ng-click="increment(post);">Increment</a>
相关文章:
- JQuery使计数器每次更改时都会增加
- 增加jQuery中的计数器
- 创建新数组或增加计数器
- 在流星应用程序中不增加 int 的计数器
- 增加营业时间到coutdown计数器
- 单击按钮时增加计数器值
- ReactJS中计数器增加的显示元素
- 如何在点击时增加计数器
- 单击按钮时增加计数器值,然后重复使用该值
- 单击按钮时增加计数器值
- 如何在javascript中增加类内的计数器值
- 在类创建/追加时增加计数器的值1
- 如何制作一个连续的Jquery计数器,每24小时增加一个数字
- 增加时钟计数器
- 如何在回调setTimeout函数后增加计数器变量
- 如何在单击ng-repeat中的项时增加计数器
- 如何在一个新的窗口中打开一个链接在javascript和增加计数器只对按钮点击
- 如何在单击时增加计数器
- 在JavaScript中自动增加计数器
- 在特定条件下增加计数器(例如:仅当用户没有输入错误答案时)