How can you limit the value from input using Angul

2020-01-25 01:06发布

I am looking for ways to limit the value inside the input to 4 and process the 4 digit value unto my controller.

 <input type="number" class="form-control input-lg"
 ng-model="search.main" placeholder="enter first 4 digits: 09XX">
                {{ search.main | limitTo:4}}

16条回答
狗以群分
2楼-- · 2020-01-25 01:42

We can write the directive to listen to the keypress event. But for some old browsers, this event is not triggered. Thats why i created a directive in such a way that there's no dependency on browser specific events limitation. I created an angular directive to limit the number of text in the input box.

'use strict';
angular.module("appName")
.directive("limitTo", [function() {
return {
restrict: "A",
require: "ngModel",
link: function(scope, elem, attrs, ctrl) {
    var limit = parseInt(attrs.limitTo);
    ctrl.$parsers.push(function (value) {
        if (value.length > limit){
            value = value.substr(0, limit);
            ctrl.$setViewValue(value);
            ctrl.$render();
        }
        return value;
    });
 }
}}]);

Usage: <input limit-to="3" ng-model="test"/> would allow only 3 characters in input box.

查看更多
一纸荒年 Trace。
3楼-- · 2020-01-25 01:47

We can use ng-value instead:

ng-value="(minutes > 60 ? minutes = 0 : minutes)"

In html code:

<input type="text" class="form-control" ng-model="minutes" ng-maxlength="2" ng-pattern="/^[0-9]*$/" ng-value="(minutes > 60 ? minutes = 0 : minutes)"/>

This will check for the value and if it is greater than 60, it replaces the value with 0.

查看更多
SAY GOODBYE
4楼-- · 2020-01-25 01:48

Can always make a directive for it:

app.directive("limitTo", [function() {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            var limit = parseInt(attrs.limitTo);
            angular.element(elem).on("keypress", function(e) {
                if (this.value.length == limit) e.preventDefault();
            });
        }
    }
}]);

<input limit-to="4" type="number" class="form-control input-lg" ng-model="search.main" placeholder="enter first 4 digits: 09XX">
查看更多
闹够了就滚
5楼-- · 2020-01-25 01:48

In Angular Js Material we can limit input field by "maxLimit", for example we need limit of input text should b 60 character as:

maxLimit ='60'

complete code:

<form-input-field flex
                  class="input-style-1"
                    title="Quick response tag title"
                    placeholder="Write a catchy title to help users get more information on the service card"
                    form-name="parent.qrtForm"
                    show-error="showError"
                    name="title"
                    maxLength="65"
                    text-limit="65"
                    required="true"
                    ng-model="newQrt.tagName">
查看更多
地球回转人心会变
6楼-- · 2020-01-25 01:49

Use this directive if you want to restrict max length for a input field which is present as part of custom directive template. This is the native html5 way of restricting max-lenth. This will also handle the copy paste case also to restrict till the maxlength on pasting.

app.directive('inputWrapper', function() {
    return {
        restrict: 'A',
        template: "<input type='text'/>"
    };
});

app.directive('maxInputLength', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            var limit = parseInt(attrs.maxInputLength);
            angular.element(elem).find('input').attr('maxlength', limit);
        }
    };
});
<input-wrapper max-input-lenth="35"></input-wrapper>

查看更多
女痞
7楼-- · 2020-01-25 01:50

Angular material has a directive mdMaxlength, if you want to cut off the input at this length, you can use this directive:

 app.directive("forceMaxlength", [function() {
   return {
     restrict: "A",
     link: function(scope, elem, attrs) {
       var limit = parseInt(attrs.mdMaxlength);
       angular.element(elem).on("keydown", function() {
         if (this.value.length >= limit) {
           this.value = this.value.substr(0,limit-1);
           return false;
         }
       });
     }
   }
 }]);

Usage:

<input type="text" md-maxlength="30" force-maxlength=""/>
查看更多
登录 后发表回答