如何重置AngularJs中的计数

时间:2017-08-20 09:23:51

标签: javascript angularjs

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

3 个答案:

答案 0 :(得分:1)

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

Try DEMO

def read_from_cvs(filename_queue):
    reader = tf.TextLineReader()
    key, value = reader.read(filename_queue)
    record_defaults = [[] for col in range((num_attributes))] # no defaults, all values must be given
    attributes = tf.decode_csv(value, record_defaults=record_defaults)
    features = tf.stack(attributes[1:-1])
    labels = tf.stack(attributes[-1])
    return features, labels

def input_pipeline(filename = 'dataset.csv', batch_size = 30, num_epochs=None):
    filename_queue = tf.train.string_input_producer(filename, num_epochs=num_epochs, shuffle=True)
    features, labels = read_from_cvs(filename_queue)

    min_after_dequeue = 10000
    capacity = min_after_dequeue + 3 * batch_size
    feature_batch, label_batch = tf.train.shuffle_batch(
        [features, labels], batch_size=batch_size, capacity=capacity,
        min_after_dequeue=min_after_dequeue)
    return feature_batch, label_batch

答案 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
  };
}

答案 2 :(得分:0)

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
};