无法传递数组输入掩码插件的角度。也许有人能帮我解决这个问题。
angular.module('myproject.directives').
directive('inputMask', function() {
return {
restrict: 'A',
scope: {
inputMask: '@'
},
link: function(scope, el, attrs) {
$(el).inputmask(attrs.inputMask);
}
};
});
<input type="text" input-mask="{'mask': '9{4} 9{4} 9{4} 9{4}[9]', 'autoUnmask': 'true'}" />发布于 2016-02-05 15:18:30
属性值将返回一个字符串,而不是传递给插件所需的对象。
您可以切换引号,以便字符串是有效的JSON,然后将json解析为对象
<input type="text" input-mask='{"mask": "9{4} 9{4} 9{4} 9{4}[9]", "autoUnmask": "true"}' />JS
.directive('inputMask', function() {
return {
restrict: 'A',
scope: {
inputMask: '@'
},
link: function(scope, el, attrs) {
var mask =JSON.parse(attrs.inputMask);
$(el).inputmask(mask);
}
};
})但实际上,这样做要简单得多,不将字符串放在html中,而是将对象引用从控制器传递到孤立的作用域。
发布于 2016-02-05 15:31:36
只需使用scope.$eval方法执行inputMask属性中的表达式:
angular.module('myproject.directives')
.directive('inputMask', function() {
return {
restrict: 'A',
scope: {
inputMask: '@'
},
link: function(scope, el, attrs) {
$(el).inputmask(scope.$eval(attrs.inputMask));
}
};
});
<input type="text" input-mask="{'mask': '9{4} 9{4} 9{4} 9{4}[9]', 'autoUnmask': 'true'}" />https://stackoverflow.com/questions/35227271
复制相似问题