material icon indicating copy to clipboard operation
material copied to clipboard

Prevent md-switch default behaviour with ng-click

Open jaymanned opened this issue 11 years ago • 9 comments

I've been unable to find a way to prevent the md-switch default behaviour when clicked using ng-click. I tried with $event.stopPropagation() and $event.preventDefault() with no luck. Is this the correct behaviour?

Any workaround available?

jaymanned avatar Sep 30 '15 21:09 jaymanned

@jaymanned What's the use-case for clicking on a switch have not having it toggle?

jelbourn avatar Oct 01 '15 00:10 jelbourn

We use the switch to apply/remove a filter. But once the switch is on, clicking on the switch opens the filter panel to allow modifying it (and removing it).Then depending on the user action we programmatically switch it on or off.

It's a hijack, true, but nonetheless almost every standard form control allows to prevent its default behaviour to implement these kind of features.

jaymanned avatar Oct 01 '15 08:10 jaymanned

@jelbourn Any workaround you can think of?

jaymanned avatar Oct 03 '15 10:10 jaymanned

@jaymanned As a workaround, you could render a transparent element on top of the switch that intercepts clicks to do what you need.

jelbourn avatar Oct 05 '15 13:10 jelbourn

@jelbourn Actually I came up with a quick and dirty workaround to implement my 3 state switch combining the ng-click and ng-change events. That way I can determine if the switch has been dragged on/off or clicked on/off.

For dragging, I respect the user's intention and don't hijack the control. But when clicking on/off, I intercept the ng-change event and show a filter panel that allows to set/modify the filter. If the user clicks on Apply then I programatically set on the switch by changing the model. If he/she clicks on Remove filter I switch it off. That simple.

jaymanned avatar Oct 05 '15 15:10 jaymanned

A usecase for this is that the switch controls an asynchronous event that could fail.

davetclark avatar Feb 05 '16 18:02 davetclark

@jaymanned Why not create a fake switch as a workaround? I met this problem too, just switch hide/show of two pictures is much easier than implement a 3 states switch

ds009 avatar May 24 '16 06:05 ds009

@ds009 as I mentioned above my little hijacking scheme worked out nice. The advantage is having a nice switch user can actually slide. You can see this as a petty micro-interaction, but I think these little things sum up to provide an pleasurable experience which is one of the goals behind material design philosophy.

jaymanned avatar May 24 '16 18:05 jaymanned

one use case is, we want to pop up a confirmation modal before the toggle actually happens. e.g. we use md-switch to enable/disable a service which cause irreversible result, so before md-switch actually toggles, we want to let the user confirm this operation. I have tried my workarounds but md-swtich click event seems to be unpreventable.

duoli avatar Jan 18 '17 09:01 duoli