Scenario : I am working with Angular js directive. I am working on making elements(i.e all anchor tags and buttons, apart from that, divs which consist ng-click as an attribute) disable. For that, I am using two directives. One for taking all elements which satisfies above mentioned condition and second directive for overriding that element's click event.
Problem : Till now, I got success in getting elements and applying directive dynamically on those elements. After that, I want to disable click event. My Question is, How can I make ng-click null and then once my conditions satisfies I have to execute same ng-click. Note: I am using $compile to compile html of the content. disableElement directive gets all elements and set ng-click attribute as null.But somehow,it is not working.
Goal: Making all element's ng-click disable.
// Disable Element Directive
app.directive('disableElement', ['$timeout','$compile',($timeout,$compile)=> {
return {
restrict: 'AE',
priority: 1,
link: (scope, element, attr)=> {
$timeout(()=> {
if (condition satisfies get all elements and apply directive dynamically) {
var allDivs = angular.element(element).find("*");
var clickAction;
for (var i = 0; i < allDivs.length; i++) {
var currentElement = allDivs[i];
if (currentElement.attributes.length > 0) {
for (var j = 0; j < currentElement.attributes.length; j++) {
if (currentElement.nodeName == "a" || currentElement.attributes[j].name == "ng-click") {
//Gettting click action
clickAction = currentElement.getAttribute("ng-click");
//making ng-click null
currentElement.setAttribute("ng-click",null);
//applying directive dynamically
currentElement.setAttribute("ng-demo","");
$compile(currentElement)(scope);
}
}
}
}
}
}, 1000);
}
};
}]);
//ng-demo directive which overrides ng-click
app.directive('ngDemo',function() {
return {
restrict: 'A',
link: (scope, element, attr) => {
var clickAction = attr.ngClick;
element.bind('click', (e)=> {
e.preventDefault();
});
}
};
});
<some-element ng-click="someFunction()">and insidesomeFunction():function someFunction() { if (condition.holds) { return; } else { /* do something */ } }Then you only need to setcondition.holdstotrueandngClicks will be effectively disabled.