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

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

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

      输入模型在更改时从 Integer 更改为 String

      时间:2023-09-07

        <tfoot id='NlFUQ'></tfoot>

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

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

                  <tbody id='NlFUQ'></tbody>

                本文介绍了输入模型在更改时从 Integer 更改为 String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                具有一种基于输入模型的价格范围/评级功能.在加载时,当它从后端设置时,它以一个整数开始,但是当你输入它时,它会变成一个字符串.Angular 中是否有任何方法可以将输入的值声明为整数?

                Have a kind of price range/rating functionality based on an inputs model. On load, when it's set from the backend, it starts off as an integer, but when you type in it, it changes to a string. Is there any way in Angular to declare the value of an input as integer?

                HTML:

                <input type="text" name="sellPrice" id="sellPrice" class="sell-price" data-ng-model="menu.totalPrice" data-ng-change="updateMenuPriceRange()"required>
                

                JS:

                $scope.updateAggregatePricing();
                
                if ($scope.menu.totalPrice === 0) {
                    $scope.menuPriceRange = "";
                } else if ($scope.menu.totalPrice < 10) {
                    $scope.menuPriceRange = "$";
                } else if ($scope.menu.totalPrice >= 10 && $scope.menu.totalPrice <= 12.50) {
                    $scope.menuPriceRange = "$$";
                } else if ($scope.menu.totalPrice >= 12.51 && $scope.menu.totalPrice < 15) {
                    $scope.menuPriceRange = "$$$";
                } if ($scope.menu.totalPrice >= 15) {
                    $scope.menuPriceRange = "$$$$";
                } else {
                    $scope.menuPriceRange = "";
                }
                

                推荐答案

                我知道我迟到了,但我想我会发布这个答案,因为其他人可能仍在寻找替代方案.

                I know I'm late but I figured I'd post this answer as other people might still be searching for alternatives.

                您可以通过使用 AngularJS 指令链接函数来解决这个问题.代码:

                You could solve this by using the AngularJS directive linking function. The code:

                var myMod = angular.module('myModule', []);
                
                myMod.directive('integer', function(){
                    return {
                        require: 'ngModel',
                        link: function(scope, ele, attr, ctrl){
                            ctrl.$parsers.unshift(function(viewValue){
                                return parseInt(viewValue, 10);
                            });
                        }
                    };
                });
                

                然后,您将在输入元素上使用此指令,以确保您输入的任何值都被解析为整数.(很明显,这个例子没有验证输入以确保输入的内容实际上是一个整数,但是您可以使用正则表达式轻松实现这一点)

                You would then use this directive on an input element to make sure that any value you enter, gets parsed to an integer. (obviously this example doesn't validate the input to make sure that what was entered is in fact an integer, but you could easily implement this with the use of regular expressions for example)

                <input type="text" ng-model="model.value" integer />
                

                关于这个主题的更多信息可以在表单上的 AngularJS 文档中找到,就在自定义验证"部分周围:http://docs.angularjs.org/guide/forms .

                More information about this topic can be found on the AngularJS docs on forms, right around the section of "Custom validation": http://docs.angularjs.org/guide/forms .

                更新了 parseInt() 调用以包含基数 10,如 adam0101 所建议的

                Updated parseInt() call to include the radix 10, as suggested by adam0101

                这篇关于输入模型在更改时从 Integer 更改为 String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 JavaScript 限制最小值/最大值之间的数字 下一篇:三.js 生成UV坐标

                相关文章

                最新文章

              • <tfoot id='5lL22'></tfoot>

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

                  <legend id='5lL22'><style id='5lL22'><dir id='5lL22'><q id='5lL22'></q></dir></style></legend>

                  <small id='5lL22'></small><noframes id='5lL22'>

                    • <bdo id='5lL22'></bdo><ul id='5lL22'></ul>