I am trying to write an Angular2 attribute directive to change the behavior of some elements. More specifically, I want to apply an attribute to certain elements that have click handlers and prevent the execution of a related function under certain conditions.
So now I have an element, for example:
<button (click)="onClick(param1, param2)"></button>
onClick is a function declared on a component in which the host button element does some work.
I would like to write something like:
<button (click)="onClick(param1, param2)" online-only></button>
and have such a directive as:
@Directive({ selector: '[online-only]', }) export class OnlineOnlyDirective { @HostListener('click', ['$event']) onClick(e) { if(someCondition){ e.preventDefault(); e.stopPropagation(); } } }
But first, the click handler is executed, thereby preventing my directive from stopping its execution.
The second approach that I was thinking about was to replace (click) my own handler eg ([onlineClick] = "onClick") and execute the passed function when the directive deems necessary, but in this way I can not pass the parameters to the onClick function and a little weirder look.
Do you have any thoughts on something like this?
javascript angular ionic2 angular2-directives
masimplo
source share