【发布时间】:2017-01-14 15:53:05
【问题描述】:
如何使用 angular2 将电话号码字段限制为 10 个字符。 我尝试使用 ng-maxlenth,但它只能在浏览器中工作,而不能在 android 设备中工作。
我找到了一个使用 angular 1 的代码 sn-p。但是我如何使用 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">
【问题讨论】:
标签: angular typescript ionic-framework ionic2 ionic3