Marco Santos Marco Santos - 20 days ago 5
AngularJS Question

change in custom directive ngClick event/function

i have a custom directive that i need to insert some login, basically i need in my custom directive prevent from being fired a function in ngClick, than depending in some logic that is made i will be calling a modal or the function/event that was in first place in the ngClick.
I been searching online for something similiar but i cant find it, maybe my terms search arent right.

How can i get the value from ngclick to store it and later fire it again?

directive:

link: function($scope, element, attrs, controller) {
element.on( 'click', function( evt ){
evt.preventDefault()
evt.stopImmediatePropagation();


$(element).on('click', function(e) {
return $scope[controllerName][clickTarget];
});

if(somelogic) {
evt.preventDefault()
evt.stopImmediatePropagation();



$uibModal.open({
animation: true,
backdrop: 'static',
keyboard: false,
templateUrl: 'template/path',
size: 'lg',
controller: 'SomeController',
controllerAs: 'someCtrl',


});

}else{

//get the event/function from ngClick to be fire up
}
});
}


html:

<a ng-click="ctrl.modal();">Link</a>

Answer

You can do it HERE is sample. In this directive i am opening dialogbox iif checkbox is checked.

Your HTML will look like where "click-me" is my custom directve

<a class="btn btn-default" href="#" click-me is-checked="isChecked">Show a Modal</a>

And directive will be

link: function(scope, element, attrs){
           element.on('click', function(e) {
                if(scope.isChecked)
              {
                console.log('if');
                $uibModal.open({
                        animation: true,
                    backdrop: 'static',
                    keyboard: false,
                    templateUrl: 'modal.html',
                    controller: "ModalController"
                });
              }
              else
              {
                console.log('else ');
                scope.clickMe();
              }
           });