angularjs:只允许在文本框中输入数字 [英] angularjs: allows only numbers to be typed into a text box

查看:36
本文介绍了angularjs:只允许在文本框中输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 angularjs 中是否有任何可用的功能只允许将数字输入到文本框中 like

解决方案

此功能正是您所需要的.http://docs.angularjs.org/api/ng.directive:input.number

您可以将 jquery 插件包装到指令中.我在这里创建了一个示例:http://jsfiddle.net/anazimok/jTJCF/

HTML:

<div><input type="text" min="0" max="99" number-mask="" ng-model="message"><button ng-click="handleClick()">广播</button>

CSS:

.ng-invalid {边框:1px纯红色;}

JS:

//声明一个模块var app = angular.module('myApp', []);app.directive('numberMask', function() {返回 {限制:'A',链接:函数(范围,元素,属性){$(元素).数字();}}});

In angularjs is there any functionality available that allows only numbers to be typed into a text box like

解决方案

This functionality just what you need. http://docs.angularjs.org/api/ng.directive:input.number

EDIT:

You can wrap the jquery plugin into directive. I created an example here: http://jsfiddle.net/anazimok/jTJCF/

HTML:

<div ng-app="myApp">
    <div>
        <input type="text" min="0" max="99" number-mask="" ng-model="message">
            <button ng-click="handleClick()">Broadcast</button>
    </div>

</div>

CSS:

.ng-invalid {
    border: 1px solid red;
}

JS:

// declare a module
var app = angular.module('myApp', []);

app.directive('numberMask', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            $(element).numeric();
        }
    }
});

这篇关于angularjs:只允许在文本框中输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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