ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

ui-sref not working with ng-click (angular-touch) if used without jQuery

Open scheffield opened this issue 12 years ago • 13 comments

Hi,

I have a hard time removing jQuery from our app. The main problem: a simple link combining ng-click with ui-sref:

<a data-ui-sref="main2" data-ng-click="reportClick()">Link</a>

The result: ng-click fires, ui-sref does not. This only happens when using angular-touch, without jQuery as a dependency and a mobile browser, or with the emulation of a mobile device in the new chrome 32 (I used an Nexus 4 as emulation target).

A running example: http://jsfiddle.net/scheffield/AEfMm/ To see the effect: http://jsfiddle.net/scheffield/AEfMm/show in a mobile browser

I already did a little research and found out that the event object is broken:

element.bind("click", function(e) {
        var button = e.which || e.button;

        if ((button === 0 || button == 1) && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
          // HACK: This is to allow ng-clicks to be processed before the transition is initiated:
          $timeout(function() {
            scope.$apply(function() {
              $state.go(ref.state, params, { relative: base });
            });
          });
          e.preventDefault();
        }
      });

It is an object containing nothing but two functions implemented as angular.noop.

Any ideas?

scheffield avatar Jan 27 '14 10:01 scheffield

Any news on this?

homerjam avatar Nov 25 '14 12:11 homerjam

Submitted PR #1580

homerjam avatar Nov 25 '14 13:11 homerjam

Any ideas?

Submit a bug against angular-touch?

As I commented in @homerjam's PR, the fact that angular-touch breaks basic event handling isn't something a library like UI Router should have to know or care about.

What you're essentially suggesting is that every Angular module which implements a directive that binds click events needs to know about the ways in which angular-touch is broken. That just does not make any sense to me.

nateabele avatar Dec 05 '14 04:12 nateabele

Did some digging. See https://github.com/angular/angular.js/issues/9872 and https://github.com/angular/angular.js/issues/6001 (which, it looks like you opened, so there you go).

nateabele avatar Dec 05 '14 04:12 nateabele

I turned my PR into a separate module https://github.com/homerjam/angular-ui-sref-fastclick

homerjam avatar Dec 08 '14 17:12 homerjam

@homerjam Nice. Feel free to update the wiki accordingly.

nateabele avatar Dec 08 '14 18:12 nateabele

If anyone else reached here like I did because of a completely different issue. Here is the problem and the fix.

Precedence of ng-click and ui-sref has changed between versions.

Before, ui-sref worked before ng-click, so event.preventDefault() on ng-click did not stop ui-sref from firing, now it does.

I am sure this must be somewhere in the log, but this is the first page result so it might help someone.

ofpiyush avatar Jan 20 '15 19:01 ofpiyush

Thanks @ofpiyush, so the solution would be to remove $event.preventDefault() from the handelr fired by ng-click?

LeonardoGentile avatar Mar 05 '15 14:03 LeonardoGentile

yes

ofpiyush avatar Mar 07 '15 18:03 ofpiyush

Hi. @ofpiyush, @LeonardoGentile, where should I remove $event.preventDefault()? Thanks.

hernancete avatar Apr 06 '15 23:04 hernancete

@hernancete the handler of ng-click

If your html looks like

<a ng-click="myClickHandler($event)" ui-sref="my.state">My State</a>

and your controller has

$scope.myClickHandler = function(event) {
    event.preventDefault();
    // Do awesome click-ey things
}

Remove the line

event.preventDefault();

To make it

$scope.myClickHandler = function(event) {
    // Do awesome click-ey things
}

ofpiyush avatar Apr 07 '15 20:04 ofpiyush

Thanks a lot for the answer.

hernancete avatar Apr 07 '15 20:04 hernancete

You don't have to write ng-click, only ui-sref will also work, Use latest or at least 0.3.2 for ui-router

<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.2/angular-ui-router.min.js"></script>

erBhushanPawar avatar Jul 25 '17 03:07 erBhushanPawar