我试图使用输入掩码jQuery插件作为指令,但在Chrome的控制台错误中得到以下错误。
TypeError: Cannot read property 'length' of undefined我的代码
JS
var app = angular.module('app', ['ngResource']);
app.directive('inputMask', function(){
return {
restrict: 'A',
link: function(scope, element){
element.mask();
}
}
})<input type="text" class="form-control input-mask" data-input-mask data-mask="{mask: 00/00/0000}" placeholder="eg: 23/05/2014">http://plnkr.co/edit/Kp3SYS0cbIfVm1gTwtE0?p=preview
请帮我把这个修好。
发布于 2015-05-26 19:57:28
长度错误是因为element.mask()方法需要一个带有要使用的掩码字符串的属性。(在本个案中为“00/00/0000”)。所以你必须改变一些事情,首先你的指令:
var app = angular.module('app', ['ngResource']);
app.directive('inputMask', function(){
return {
restrict: 'A',
scope: {
inputMask: '='
},
link: function(scope, element){
element.mask(scope.inputMask.mask);
}
}
})然后在html中,您可以在元素中设置掩码。
<input type="text" class="form-control" data-input-mask="{mask: '00/00/0000'}" placeholder="eg: 23/05/2014">这是一个柱塞工作:
http://plnkr.co/edit/BbJtsF9mWx4n29CfZajF?p=preview
https://stackoverflow.com/questions/30467017
复制相似问题