Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scheffield opened this issue Jan 27, 2014 · 13 comments
Closed

Comments

@scheffield
Copy link

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?

@homerjam
Copy link

Any news on this?

@homerjam
Copy link

Submitted PR #1580

@nateabele
Copy link
Contributor

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
Copy link
Contributor

Did some digging. See angular/angular.js#9872 and angular/angular.js#6001 (which, it looks like you opened, so there you go).

@homerjam
Copy link

homerjam commented Dec 8, 2014

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

@nateabele
Copy link
Contributor

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

@ofpiyush
Copy link

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.

@LeonardoGentile
Copy link

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

@ofpiyush
Copy link

ofpiyush commented Mar 7, 2015

yes

@hernancete
Copy link

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

@ofpiyush
Copy link

ofpiyush commented Apr 7, 2015

@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
}

@hernancete
Copy link

Thanks a lot for the answer.

@erBhushanPawar
Copy link

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants