<bdo id='m3BsM'></bdo><ul id='m3BsM'></ul>

    <i id='m3BsM'><tr id='m3BsM'><dt id='m3BsM'><q id='m3BsM'><span id='m3BsM'><b id='m3BsM'><form id='m3BsM'><ins id='m3BsM'></ins><ul id='m3BsM'></ul><sub id='m3BsM'></sub></form><legend id='m3BsM'></legend><bdo id='m3BsM'><pre id='m3BsM'><center id='m3BsM'></center></pre></bdo></b><th id='m3BsM'></th></span></q></dt></tr></i><div id='m3BsM'><tfoot id='m3BsM'></tfoot><dl id='m3BsM'><fieldset id='m3BsM'></fieldset></dl></div>
  1. <tfoot id='m3BsM'></tfoot>

    1. <small id='m3BsM'></small><noframes id='m3BsM'>

      <legend id='m3BsM'><style id='m3BsM'><dir id='m3BsM'><q id='m3BsM'></q></dir></style></legend>

      使用 angularjs,如何在文本框上执行数学运算并在输入值时查看结果?

      Using angularjs, how to perform math operations on textboxes and see result as the values are being typed in?(使用 angularjs,如何在文本框上执行数学运算并在输入值时查看结果?)

          <tbody id='K1seV'></tbody>

            <i id='K1seV'><tr id='K1seV'><dt id='K1seV'><q id='K1seV'><span id='K1seV'><b id='K1seV'><form id='K1seV'><ins id='K1seV'></ins><ul id='K1seV'></ul><sub id='K1seV'></sub></form><legend id='K1seV'></legend><bdo id='K1seV'><pre id='K1seV'><center id='K1seV'></center></pre></bdo></b><th id='K1seV'></th></span></q></dt></tr></i><div id='K1seV'><tfoot id='K1seV'></tfoot><dl id='K1seV'><fieldset id='K1seV'></fieldset></dl></div>
            • <legend id='K1seV'><style id='K1seV'><dir id='K1seV'><q id='K1seV'></q></dir></style></legend>

              <small id='K1seV'></small><noframes id='K1seV'>

            • <tfoot id='K1seV'></tfoot>
                <bdo id='K1seV'></bdo><ul id='K1seV'></ul>

              • 本文介绍了使用 angularjs,如何在文本框上执行数学运算并在输入值时查看结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个带有 2 个文本框的 HTML 文件,一个用于价值,另一个用于数量.底部的结果文本将值乘以数量并显示结果.

                I have an HTML file with 2 textboxes, one for value and the other for quantity. The result text at the bottom multiplies value with quantity and show the result.

                目的是在屏幕上显示所有成对的文本框行的总和.为此,我有一个添加新"按钮,它不断添加额外的文本框对.

                The intention is to show the sum of all the rows of pairs of textboxes on the screen. To that end, I have an "add new" button which keeps adding additional pairs of textboxes.

                出现在 HTML 中的第一组文本框反映了包含属性val"和qty"的对象numbers"数组的大小.相同的值绑定到文本框.

                The first set of textboxes that appear on the HTML, reflect the size of the "numbers" array of objects containing properties "val" and "qty". The same values are bound to the textboxes.

                但是,只有第一组值会添加到屏幕上.当我不断添加新文本框并输入新值时,结果的值应该会相应改变,但它根本不会.

                However, only the first set of values are added on screen. As I keep adding new textboxes and entering new values, the value of the result should change accordingly, but it simply doesn't.

                HTML 代码

                    <div ng-app="adder" ng-controller="addcontrol">
                        <table>
                        <tr>
                            <th>Value</th><th>Quantity</th>
                        </tr>
                        <tr ng-repeat="number in numbers">
                            <td><input type="text" ng-model="number.val"></td>
                            <td><input type="text" ng-model="number.qty"></td>
                            <td><input type="button" ng-click="deleteNumber($index)" value= "Delete"></td>pp',[]);
                        </tr>
                        </table>
                        <input type="button" ng-click="add()" value="Add new">Result : {{sum}}
                    </div>
                </body>
                </html>
                

                Javascript

                var myapp = angular.module('adder', []);
                myapp.controller('addcontrol',function($scope){
                $scope.numbers = [
                    {val:100,
                     qty:200,
                    }
                
                ];
                
                $scope.add = function()
                {
                    $scope.numbers.push({val:0,qty:0});
                };
                
                $scope.deleteNumber = function(val)
                {
                    numbers.splice(val, 1);
                };
                var result=0;
                angular.forEach($scope.numbers, function(num){
                    result+=(num.val * num.qty);
                
                });
                $scope.sum = result;
                

                });

                我在这里做错了什么?

                推荐答案

                在你的代码中,总和的计算只会执行一次.

                In your code, the calculation of the sum would only be executed once.

                您需要添加范围的监视或将函数绑定到 ng-change 事件,以便在更改数字时保持总和更新.

                You need to add a watch of the scope or bind a function to ng-change event in order to keep the sum updated while you change the numbers.

                例如,你可以这样做:

                <div ng-app="adder" ng-controller="addcontrol">
                    <table>
                    <tr>
                        <th>Value</th><th>Quantity</th>
                    </tr>
                    <tr ng-repeat="number in numbers">
                        <td><input type="text" ng-change="update()" ng-model="number.val"></td>
                        <td><input type="text" ng-change="update()" ng-model="number.qty"></td>
                        <td><input type="button" ng-click="deleteNumber($index)" value= "Delete"></td>pp',[]);
                    </tr>
                    </table>
                    <input type="button" ng-click="add()" value="Add new">Result : {{sum}}
                </div>
                

                还有:

                var myapp = angular.module('adder', []);
                myapp.controller('addcontrol', function($scope) {
                  $scope.numbers = [{
                      val: 100,
                      qty: 200,
                    }
                
                  ];
                
                  $scope.add = function() {
                    $scope.numbers.push({
                      val: 0,
                      qty: 0
                    });
                  };
                
                  $scope.deleteNumber = function(val) {
                    numbers.splice(val, 1);
                    $scope.update();
                  };
                
                  $scope.update = function() {
                    var result = 0;
                    angular.forEach($scope.numbers, function(num) {
                      result += (num.val * num.qty);
                    });
                    $scope.sum = result;
                  };
                });
                

                这篇关于使用 angularjs,如何在文本框上执行数学运算并在输入值时查看结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Embed YouTube video - Refused to display in a frame because it set #39;X-Frame-Options#39; to #39;SAMEORIGIN#39;(嵌入 YouTube 视频 - 拒绝显示在框架中,因为它将“X-Frame-Options设置为“SAMEORIGIN)
                Stop embedded youtube iframe?(停止嵌入 youtube iframe?)
                YouTube iframe API: how do I control an iframe player that#39;s already in the HTML?(YouTube iframe API:如何控制 HTML 中已有的 iframe 播放器?)
                Switch/toggle div (jQuery)(切换/切换 div (jQuery))
                How to avoid name clashes in JavaScript widgets(如何避免 JavaScript 小部件中的名称冲突)
                Widget -what to do and what not(小部件 - 做什么和不做什么)
                  <bdo id='od2v5'></bdo><ul id='od2v5'></ul>
                • <small id='od2v5'></small><noframes id='od2v5'>

                  <tfoot id='od2v5'></tfoot>
                  <i id='od2v5'><tr id='od2v5'><dt id='od2v5'><q id='od2v5'><span id='od2v5'><b id='od2v5'><form id='od2v5'><ins id='od2v5'></ins><ul id='od2v5'></ul><sub id='od2v5'></sub></form><legend id='od2v5'></legend><bdo id='od2v5'><pre id='od2v5'><center id='od2v5'></center></pre></bdo></b><th id='od2v5'></th></span></q></dt></tr></i><div id='od2v5'><tfoot id='od2v5'></tfoot><dl id='od2v5'><fieldset id='od2v5'></fieldset></dl></div>
                      <tbody id='od2v5'></tbody>

                      <legend id='od2v5'><style id='od2v5'><dir id='od2v5'><q id='od2v5'></q></dir></style></legend>