我正在寻找将输入中的值限制为4并将4位数字值处理给我的控制器的方法。
<input type="number" class="form-control input-lg" ng-model="search.main" placeholder="enter first 4 digits: 09XX"> {{ search.main | limitTo:4}}
可以随时为其发出指令:
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">