- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在编写指令以在文本区域字段(HTML 版本 < 5)上强制执行最大长度。我正在使用 AngularJS。因此,我想以正确的 AngularJS 方式完成它。然而,当我按照我的理解去做“Angularesque”时,我无法完全让它发挥作用。
(function () {
'use strict';
var myAppModule = angular.module('myApp', []);
myAppModule.controller('MyController', function($scope) {
$scope.textareaText = "";
});
myAppModule.directive('myMaxlength', ['$compile', '$log', function($compile, $log) {
return {
priority: 10000,
restrict: 'A',
require: 'ngModel',
compile: function (elem, attrs) {
elem.attr("ng-trim", "false");
return function(scope, linkElem, linkAttrs, ctrl) {
var maxlength = parseInt(linkAttrs.myMaxlength, 10);
ctrl.$parsers.push(function (value) {
$log.info("In parser function value = [" + value + "].");
if (value.length > maxlength)
{
$log.info("The value [" + value + "] is too long!");
value = value.substr(0, maxlength);
ctrl.$setViewValue(value);
ctrl.$render();
$log.info("The value is now truncated as [" + value + "].");
}
return value;
});
};
}
};
}]);
})();
这几乎可以工作(请参阅 JSFIDDLE here 注意:此 fiddle 已经使用下面选择的解决方案进行了更新,但上面的代码仍然存在此处提到的问题。)。它正确地限制了进入模型的字符,并将其渲染回显示器。但是,您可以继续输入空格以超过指定的最大长度。多余的空间不会进入模型,但会使显示变得困惑。我相信这与 ng-trim 有关。如您所见(在代码片段中),我在编译函数中设置属性,但为时已晚。有些人建议在你的编译函数中手动调用编译来强制 AngularJS 识别动态添加的属性,但是“出现”创建另一个副本并且没有解决显示问题。将 ng-trim="false"
放在 textarea 元素上是可行的,但我想要一个干净的解决方案,用户需要做的就是添加一个指定最大长度的属性。
这是另一个例子(参见 JSFIDDLE here),在我看来它并不像“Angularesque”,但完成了工作。
(function () {
'use strict';
var myAppModule = angular.module('myApp', []);
myAppModule.controller('MyController', function($scope) {
$scope.textareaText = true;
});
myAppModule.directive('myMaxlength', ['$log', function($log) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var maxlength = parseInt(attrs.myMaxlength, 10);
$log.info("maxlength=[" + maxlength + "]");
elem.bind('keydown', function (event) {
$log.info("elem.val().length=[" + elem.val().length + "]");
if (elem.val().length >= maxlength)
{
// Void event Except backspace
if (event.keyCode != 8){
event.preventDefault();
}
}
});
}
};
}]);
})();
那么,在文本区域字段上实现最大长度的真正“Angularesque”方式是什么?提前致谢!
最佳答案
您可以在 html 文件的输入字段上使用 ngMaxlength 参数(此处的文档:https://docs.angularjs.org/api/ng/directive/input)
例如
<input type="text" ng-model="myRestrictedText" ng-maxlength="10"/>
关于javascript - 在 textarea 字段上实现 maxlength 的真正 "Angularesque"方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24964871/
我需要以某种方式将模型的 max_length 约束 Hook 到 Form 对象中。 假设我定义了一个带有字段的模型:name = models.CharField(max_length=30)现在
“On input while maxlength is active trigger even if maxlength is reached”,这是on input事件的经典用法。当达到 maxl
我正在尝试配置一个 TextView,它的 maxLines=3 并且它的每一行都有 maxLenght=30。 感谢您的帮助。 最佳答案 您可以对实际的 30 个字符使用 maxWidth 函数(平
在表单组验证中,我尝试应用输入最大长度,但是,这个最大长度取决于表单中的其他操作。如果它们改变,也改变 maxMsgLength 的属性值。 消息:['', Validators.maxLength(
我有一个 iOS 应用程序,我在其中使用 NSInputStream(基于 CFReadStreamRef)从网络套接字读取数据。我不断从服务器获取数据,并不断读取和处理它(使用 read:bytes
我的输入字段包含数字和特殊字符,例如逗号和点在计算字段的 maxLength 时,我想跳过特殊字符。 我不想限制特殊字符。 预期输出应为:- 1,234(总长度:- 4) Userna
我只是在忙着弄清楚 cakephp,看起来它会很有趣。 我有一个表单输入,如下所示: echo $form->input('Campaign.title', array('maxLength'=>'7
旧的 ng-maxlength="5"用于触发字段错误,但会继续允许用户输入。 maxlength="5"似乎在阻止输入。当我的表格上有 novalidate 时,这样做是否有 Angular ?当我
我对 AllenNLP 还很陌生,我目前正在使用它的预训练问答模型。我想知道它是否有 channel 长度限制以确保其性能?我知道 BERT 的最大长度为 512,并且会截断更长的段落。 我在 All
我正在处理 TFS 流程模板的自定义工作项,但收到错误指示我用于组控件的标签超过 MaxLength 值。谁能告诉我我可以贴在群组标签上的最大长度是多少? 最佳答案 如您所见,答案是 80。这是官方文
下面是我在注册和登录表单上使用的代码,限制用户输入有限数量的字符: $( "#limit1" ).attr('maxlength','11').on('input', function() { if
我使用的是 Entity Framework 4.3.1,代码优先,并在 SQL Profiler 中查看正在生成的查询。这是一个例子: 如您所见 - 它将参数指定为 nvarchar(max) 但在
我在互联网上发现输入的最大长度是: 524288 个字符 我找不到有关 textArea 的任何信息。 我需要传递一个最多 1 500 000 个字符的 var,因此我尝试找到类似于输入文本或可用于此
使用 jQuery 版本 1.2.3,我尝试在具有属性“maxlength”的 textarea 元素之后添加节点,但它不起作用: $("textarea[@maxlength]").after("A
我使用 Javascript 添加新的文本字段: var input = document.createElement("input"); input.type = "text"; input.nam
好的,所以我正在构建一个自定义注册页面,当然还涉及到 javascript 输入验证。 在该验证中,我使用了 maxlength 属性来验证输入的长度 - 这是一种双重安全,但很容易获得我认为的元素的
基本上,我正在向所有文本框动态添加 maxLength,但我想向那些没有 maxLength 的文本框添加默认 maxLength属性集。 如何使用 jquery、javascript 完成此操作?
我的 html 页面有问题 这里是页面的简化版本 function maxLength() {
我对 ASP.NET MVC3 客户端验证有一个奇怪的问题。我有以下类(class): public class Instrument : BaseObject { public int Id
我在 Vue 中有一个表单,由于电话号码输入的样式原因,它有一些组合输入。 我的问题是用户必须点击 Tab 键才能转到电话号码的下一个输入字段。 有没有办法检查当前和输入的最大长度,并在满足时转到下一
我是一名优秀的程序员,十分优秀!