我正在寻找将输入值限制为4并将4位数值加到我的控制器的方法.
<input type="number" class="form-control input-lg" ng-model="search.main" placeholder="enter first 4 digits: 09XX"> {{ search.main | limitTo:4}}
可以随时给出一个指令:
原文链接:https://www.f2er.com/angularjs/143238.htmlapp.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">