I want to normally work my directive When I add this element by click event.
I checked that the originally declared code works as expected.
This html element is added by javascript function when be called some click event
<input type="text" reg="'^[-+]?([0-9]|[0-8][0-9]|9[0])$'" reg-check>
Here is my directive code
TEST.directive('regCheck', function(){
return {
restrict : 'A',
scope: {
reg : '='
},
link : function(scope, element, attrs){
var regEx = new RegExp(scope.reg);
var previousValue;
console.log(regEx);
$(element).on('keyup' ,function(){
var dInput = element[0].value
console.log(dInput);
if(regEx.test(dInput) || dInput =='' || dInput =='-') previousValue = dInput
else element[0].value = previousValue
});
}
}
})
I think that this problem works cause by jquery event not rendered to added element. So I try to
$(document).on('keyup', element ,function(){
var dInput = element[0].value
console.log(dInput);
if(regEx.test(dInput) || dInput =='' || dInput =='-') previousValue = dInput
else element[0].value = previousValue
});
It's not work too. What should I do?