如何在更改时显示ng个重复元素的总和

How do I display sum of ng-repeat elements on their change?

本文关键字:元素 ng 显示      更新时间:2023-09-26

我有一个ng-repeat生成的<Select>集,其中包含一个数值。每当用户更改一个<Select>时,我希望显示它们的总数值总和。{{option.score}}表示我希望分配到总数中的数值。

图像:

https://cdn.pbrd.co/images/NGg0gJn.png

模板:

    <label class="item item-input item-select" ng-repeat="criteria in tool.criterias">
        <div class="input-label">
            {{criteria.desc}}
        </div>
        <select>
            <option ng-repeat="option in criteria.options">
                {{option.score}} &mdash; {{option.answer}}
            </option>
        </select>
    </label>
    ...
    <p>Total: the sum of all the option.score's</p>

控制器:

.controller('FullToolsCtrl', function ($scope, $stateParams, Tools) {
  $scope.tool = Tools.get($stateParams.toolId);
});

数据:

{
  "criterias": [
    {
      "desc": "Complexion",
      "options": [
        {
          "answerswer": "Blue or Pale all over",
          "score": "0"
        },
        {
          "answerswer": "Blue at extremities",
          "score": "1"
        },
        {
          "answerswer": "No cyanosed body or extremities",
          "score": "2"
        }
      ]
    },
    {
      "desc": "Pulse",
      "options": [
        {
          "answerswer": "Absent",
          "score": "0"
        },
        {
          "answerswer": "< 100",
          "score": "1"
        },
        {
          "answerswer": "> 100",
          "score": "2"
        }
      ]
    },
    {
      "desc": "Grimace",
      "options": [
        {
          "answerswer": "No response to stimulation",
          "score": "0"
        },
        {
          "answerswer": "Grimace on suction or aggressive stimulation",
          "score": "1"
        },
        {
          "answerswer": "Cry on stimulation",
          "score": "2"
        }
      ]
    },
    {
      "desc": "Activity",
      "options": [
        {
          "answerswer": "Absent",
          "score": "0"
        },
        {
          "answerswer": "Some flexion",
          "score": "1"
        },
        {
          "answerswer": "Flexed arms and legs resist extension",
          "score": "2"
        }
      ]
    },
    {
      "desc": "Respiratory effort",
      "options": [
        {
          "answerswer": "Absent",
          "score": "0"
        },
        {
          "answerswer": "Weak, irregular, gasping",
          "score": "1"
        },
        {
          "answerswer": "Strong, lusty cry",
          "score": "2"
        }
      ]
    }
  ],
  "desc": "Scoring tool for neonates.",
  "name": "APGAR Score"
}

试试这样的东西:

var app = angular.module("Demo", []);
app.controller("AppController", function($scope) {
  $scope.total = 0;
  $scope.selectValue = [];
  $scope.SumValue = function() {
    $scope.total = 0
    for (var i = 0; i < $scope.selectValue.length; i++) {
      if ($scope.selectValue[i] != undefined)
        $scope.total += parseInt($scope.selectValue[i]);
    }
  }
  $scope.data = {
    "criterias": [{
      "desc": "Complexion",
      "options": [{
        "answerswer": "Blue or Pale all over",
        "score": "0"
      }, {
        "answerswer": "Blue at extremities",
        "score": "1"
      }, {
        "answerswer": "No cyanosed body or extremities",
        "score": "2"
      }]
    }, {
      "desc": "Pulse",
      "options": [{
        "answerswer": "Absent",
        "score": "0"
      }, {
        "answerswer": "< 100",
        "score": "1"
      }, {
        "answerswer": "> 100",
        "score": "2"
      }]
    }, {
      "desc": "Grimace",
      "options": [{
        "answerswer": "No response to stimulation",
        "score": "0"
      }, {
        "answerswer": "Grimace on suction or aggressive stimulation",
        "score": "1"
      }, {
        "answerswer": "Cry on stimulation",
        "score": "2"
      }]
    }, {
      "desc": "Activity",
      "options": [{
        "answerswer": "Absent",
        "score": "0"
      }, {
        "answerswer": "Some flexion",
        "score": "1"
      }, {
        "answerswer": "Flexed arms and legs resist extension",
        "score": "2"
      }]
    }, {
      "desc": "Respiratory effort",
      "options": [{
        "answerswer": "Absent",
        "score": "0"
      }, {
        "answerswer": "Weak, irregular, gasping",
        "score": "1"
      }, {
        "answerswer": "Strong, lusty cry",
        "score": "2"
      }]
    }],
    "desc": "Scoring tool for neonates.",
    "name": "APGAR Score"
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Demo">
  <div ng-controller="AppController">
    <div ng-repeat="criteria in data.criterias">
      <label>
        {{ criteria.desc }}
      </label>
      <br/>
      <select ng-model="selectValue[$index]" ng-change="SumValue()">
        <option ng-repeat="option in criteria.options" value="{{option.score}}">{{option.answer}}</option>
      </select>
    </div>
    <p>Total: the sum of all the option.score's {{ total }}</p>
  </div>
</div>

JSFiddle:https://jsfiddle.net/d42jeuvs/15/

希望这是你想要的。谢谢

ng repeat中并没有真正内置任何内容来处理此问题,而且当您希望显示您的总数时,您实际上已经超出了ng repeat构造。通常,您只需将HTML中的表达式绑定到控制器中的函数调用即可。类似于:

<p>Total: the sum of all the option.score's  {{total)()}}</p>

(其中$ctrl是您的控制器)。

然后在你的控制器中,你会有一个映射到它的函数,比如:

$scope.total=函数(){var总计=0angular.forEach($scope.criteria.options,函数(criteria){合计+=criteria.score}退货总额};

如果使用controllerAs语法,则会略有不同。