Prevent md-switch default behaviour with ng-click
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 What's the use-case for clicking on a switch have not having it toggle?
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.
@jelbourn Any workaround you can think of?
@jaymanned As a workaround, you could render a transparent element on top of the switch that intercepts clicks to do what you need.
@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.
A usecase for this is that the switch controls an asynchronous event that could fail.
@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 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.
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.