gpt4 book ai didi

javascript - 如何在 AngularJs 中重置计数

转载 作者:行者123 更新时间:2023-12-03 03:37:39 24 4
gpt4 key购买 nike

我有一个包含四个项目的列表。每个项目都有一个柜台。当我们点击该项目时,计数就会增加。我想将除单击项之外的计数器值重置为零。这是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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com