Conditional Event Binding in Angular2

2,853 views
Skip to first unread message

Cagatay Civici

unread,
Feb 17, 2016, 9:53:16 AM2/17/16
to AngularJS
Hi,

Is it possible to bind an event based on a condition.

<div (mouseenter)="handleMouseEnter($event)"></div>

But I want to bind this if a certain boolean condition is true.

Regards,

Cagatay

Eric Martinez

unread,
Feb 17, 2016, 6:54:45 PM2/17/16
to AngularJS
constructor(renderer: Renderer, elementRef: ElementRef) {


 
if(myCondition) {
       renderer
.listen(elementRef.nativeElement, 'mouseenter', () => {
          console
.log('Callback');
       
});
 
}


}


I don't think there's a way of doing it conditionally in the view. Another way is adding the condition in the function itself.

Cagatay Civici

unread,
Feb 18, 2016, 8:23:54 AM2/18/16
to AngularJS
Thanks, I ended up checking the condition inside the event.
Reply all
Reply to author
Forward
0 new messages