Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJs中重置计数_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在AngularJs中重置计数

Javascript 如何在AngularJs中重置计数,javascript,angularjs,Javascript,Angularjs,我有一份有四项的清单。每件商品都有一个柜台。当我们点击该项时,计数将增加。我想将计数器值重置为零,单击项除外。这是最新的 你可以这样试试。循环所有项目并检查单击的项目是否为当前项目:增量,其他项目设置为0 试试这个 function MyCtrl($scope) { $scope.data =jsonInfo; $scope.count = function (inc) { for(i=0; i<jsonInfo.count.length; i++){ if(

我有一份有四项的清单。每件商品都有一个柜台。当我们点击该项时,计数将增加。我想将计数器值重置为零,单击项除外。这是最新的


你可以这样试试。循环所有项目并检查单击的项目是否为当前项目:增量,其他项目设置为0

试试这个

function MyCtrl($scope) {
  $scope.data =jsonInfo;

  $scope.count = function (inc) {

  for(i=0; i<jsonInfo.count.length; i++){
      if(jsonInfo.count[i].name != inc.name){
      jsonInfo.count[i].count = 0;
      }
  }
    inc.count =  inc.count + 1
  };
}
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; 
     });
  };
}
function MyCtrl($scope) {
  $scope.data =jsonInfo;

  $scope.count = function (inc) {

  for(i=0; i<jsonInfo.count.length; i++){
      if(jsonInfo.count[i].name != inc.name){
      jsonInfo.count[i].count = 0;
      }
  }
    inc.count =  inc.count + 1
  };
}
function resetOherCount(inc) {

    jsonInfo.count.map(function(oneEle) {
        if (oneEle.name != inc.name) {
            oneEle.count = 0
        }
        return oneEle;
    });
}

$scope.count = function (inc) {

    resetOherCount(inc);
    inc.count = inc.count + 1
};