如何使用 angular2 将电话号码字段限制为 10 个字符。我尝试使用ng-maxlenth,但它只在浏览器中工作,而不在 android 设备中工作。
我找到了一个使用 angular 1 的代码片段。但是如何使用 angular2 重写相同的代码?
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">