在iphone上使用AngularJS时如何设置最大字符数

在iphone上使用AngularJS时如何设置最大字符数,iphone,angularjs,text,textbox,maxlength,Iphone,Angularjs,Text,Textbox,Maxlength,我正在尝试设置文本框中的最大字符数 <form class="form-horizontal"> <input type="text" ng-model="formtodotext" ng-model-instant maxlength="22"> <button class="btn btn-info"ng-click="addTodo()"><i class="icon-plus

我正在尝试设置文本框中的最大字符数

            <form class="form-horizontal">
            <input type="text" ng-model="formtodotext" ng-model-instant maxlength="22">
            <button class="btn btn-info"ng-click="addTodo()"><i class="icon-plus"></i>Toevoegen</button>
           </form>

托沃根
但它在iphone上不起作用,在chrome上起作用

PS:max=“22”也不起作用


很抱歉,如果我写错了,堆栈溢出noob..

如果您只想在键入的文本长度超过22时将输入标记为无效,您可以使用
ng模式

<input type="text" ng-pattern="/^.{0,22}$/" ng-model="formtodotext" ng-model-instant>

如果用户试图键入第23个字符,则阻止用户输入需要自定义指令:

app.directive('maxLength',function(){
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, el, attrs, ngModelCtrl){
      function checkLength(text) {
        var old = ngModelCtrl.$modelValue;
        if(text.length<=attrs.maxLength) {
          return text;
        }else{
          ngModelCtrl.$setViewValue(old);
          ngModelCtrl.$render();
          return old;
        }
      }
      ngModelCtrl.$parsers.push(checkLength);
    }
  }
});
app.directive('maxLength',function(){
返回{
限制:“A”,
要求:'ngModel',
链接:函数(范围、el、属性、ngModelCtrl){
函数校验长度(文本){
var old=ngModelCtrl.$modelValue;

如果(text.length我希望maxlength可以在iOS和chrome上运行。你不需要使用ng模式,因为有一个ng maxlength。它将表单标记为无效,但不会阻止用户输入更多文本



+1基本上用您的自定义指令示例回答我的问题。有效,但必须将max length=“22”更改为maxllength=“22”。实际上,
maxllength
是一个内置的HTML输入属性,完全忘记了它。尽管如此,此指令仍然有效,但您可以通过设置
max length
来使用它:
<input type="text" max-length="22" ng-model="formtodotext" ng-model-instant>