如何允许角仅整数? [英] How to allow only integer numbers in Angular?

查看:183
本文介绍了如何允许角仅整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的HTML多个输入,我希望它是一个整数,而不是一个浮点数。

所以,如果我有这样的:

 <输入类型=数字NG-模式=person.age/>

角会考虑的一个值 18.4 是有效的。我该如何解决这个问题?


解决方案

\r
\r

函数main($范围){\r
\r
     $ scope.regex ​​=/^-?[0-9][^\\.]*$/;\r
   }

\r

input.ng-dirty.ng-无效{颜色:红}

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/angularjs/1.2.23/angular.min.js\"></script>\r
&LT; D​​IV NG-应用&GT;\r
    &LT; D​​IV NG控制器=主&GT;\r
        &LT;表格名称=形式&GT;\r
&LT;输入类型=文本NG模型=person.ageNG-模式=/?^ - [0-9] [^ \\] * $ //&GT;\r
           &LT;输入类型=文本NG模型=person.age1NG-模式={{正则表达式}}/&GT;\r
        &LT; /表及GT;\r
    &LT; / DIV&GT;\r
&LT; / DIV&GT;

\r

\r
\r

试试这个

  $ scope.regex ​​=/^-?[0-9][^\\.]*$/;&LT;输入类型=数字NG-模式=person.ageNG-模式={{正则表达式}}/&GT;

I have a number input in my HTML and I want it to be an integer, not a floating point number.

So, if I have this:

<input type="number" ng-model="person.age" />

Angular will consider a value of 18.4 to be valid. How can I fix this?

解决方案

function Main($scope) {

     $scope.regex = "/^-?[0-9][^\.]*$/";
   }

input.ng-dirty.ng-invalid { color: red }

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <div ng-controller="Main">
        <form name="form">
	  <input type="text" ng-model="person.age" ng-pattern="/^-?[0-9][^\.]*$/"/>
           <input type="text" ng-model="person.age1" ng-pattern="{{regex}}"/>
        </form>
    </div>
</div>

try this

 $scope.regex = "/^-?[0-9][^\.]*$/";

<input type="number" ng-model="person.age" ng-pattern="{{regex}}"/>

这篇关于如何允许角仅整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆