在AngularJs中设置Textarea的限制

时间:2017-03-07 06:25:20

标签: angularjs

  <md-input-container class="md-block" style="margin-bottom: 6px;">
             <label style="font-size: 14px; font-weight: 400; line-height: .75;">
              Message
            </label>
            <textarea ng-model="formData.message"
              md-maxlength="10" rows="5"
              md-select-on-focus="">
            </textarea>
 </md-input-container>

嗨,我有一个textarea,我试图将最大限制设置为10个字符。但是md-maxlength无法正常工作

你可以找到演示 - https://plnkr.co/edit/jyRIABUKlNexmvQEUiIn?p=preview

谢谢:)

2 个答案:

答案 0 :(得分:2)

使用ng-maxlength查看完整详情here

示例代码

<script>
  angular.module('ngMaxlengthExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.maxlength = 5;
    }]);
</script>
<div ng-controller="ExampleController">
  <form name="form">
    <label for="maxlength">Set a maxlength: </label>
    <input type="number" ng-model="maxlength" id="maxlength" />
    <br>
    <label for="input">This input is restricted by the current maxlength: </label>
    <input type="text" ng-model="model" id="input" name="input" ng-maxlength="maxlength" /><br>
    <hr>
    input valid? = <code>{{form.input.$valid}}</code><br>
    model = <code>{{model}}</code>
  </form>
</div>

如果您不想让用户进一步输入

使用此

pp.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">

相关问题可以在here

找到

答案 1 :(得分:1)

只需使用File\Path\To\Extract.csv 代替maxlength="10"

DEMO