对于下面的文本字段,我的目标是使其具有 text-danger
类的跨度仅在 textarea
中输入的文本到达时显示maxlength
值。
据我所知, Angular 框架允许 maxlength
或 ng-maxlength
与另一个 html 元素对话,并在达到最大长度时触发操作。我该怎么做呢?
<div class="form-group">
<label class="control-label">{{ "Message" | i18next }}</label>
<textarea class="form-control" rows="3" cols="40"
ng-model="emailAsAttachment.message" name="message"
placeholder="{{ 'Optional: include a personal message' | i18next}}"
maxlength="{{MAX_INPUT_LENGTHS.description}}"></textarea>
<span class="text-danger" id="export-filename-error"
ng-show="exportForm.message.$invalid">
{{ "Maximum length of 10,000 characters exceeded." | i18next }}
</span>
</div>
最佳答案
I want to simultaneously prevent a user from exceeding the maxlength, while also detecting when it's reached.
使用 maxlength={{MAXLENGTH}}
属性来防止文本框超过指定的长度。
使用 ng-show="emailAsAttachment.message.length == MAXLENGTH"
显示已达到指定长度。
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app>
<form name="exportForm">
<textarea class="form-control" rows="3" cols="40"
ng-model="emailAsAttachment.message" name="message"
placeholder="Enter 5 or less characters"
maxlength="5">
</textarea>
</form>
<span class="text-danger" id="export-filename-error"
ng-show="emailAsAttachment.message.length == 5">
{{ "Maximum length of 5 characters reached." }}
</span>
</body>
关于html - 如何使用仅在文本区域达到其最大长度时显示的 "ng-show"切换错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366102/