我有一个包含四个项目的列表。每个项目都有一个柜台。当我们点击该项目时,计数就会增加。我想将除单击项之外的计数器值重置为零。这是Demo 。
var myApp = angular.module('myApp',[]);
var jsonInfo = {"count":[{"name":"one",count:0} ,{"name":"two",count:0},{"name":"three",count:0} ,{"name":"four",count:0}]}
function MyCtrl($scope) {
$scope.data =jsonInfo;
$scope.count = function (inc) {
inc.count = inc.count + 1
};
}
最佳答案
你可以这样尝试。循环遍历所有项目并检查单击的项目是否是当前项目:增量,其他项目设置为 0。
<强> Try DEMO
var myApp = angular.module('myApp',[]);
var jsonInfo = {"count":[{"name":"one",count:0} ,{"name":"two",count:0},{"name":"three",count:0} ,{"name":"four",count:0}]}
function MyCtrl($scope) {
$scope.data =jsonInfo;
$scope.count = function (inc) {
jsonInfo.count.forEach((item) => {
item.count = item.name === inc.name? inc.count + 1 : 0;
});
};
}
关于javascript - 如何在 AngularJs 中重置计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780529/