5

How have a link like

<a href="" ng-click="someAction()">Some text</a>

and I want to invoke ngClickaction from jQuery:

$('a').click()

But it doesn't work: someAction() not invoked. Also didn't work

$('a').trigger('click')

Is it possible to invoke someAction() from jQuery?

uladzimir
  • 5,639
  • 6
  • 31
  • 50

4 Answers4

4

Nevermind the apply cycles and timeouts, here you are:

$('a').dispatchEvent(new MouseEvent('click', {
 'view': window,
 'bubbles': true,
 'cancelable': true
}));
K-G
  • 2,799
  • 4
  • 26
  • 42
Vlad Gurovich
  • 8,463
  • 2
  • 27
  • 28
4

I think it's strange that it's not working, I get it working with both

angular.element('a[ng-click="someAction()"]')

and

$('a[ng-click="someAction()"]')

as selectors, and both click() and trigger('click') to fire the clickhandler.

See http://plnkr.co/edit/6VMavwVImXAonSp8xZrI?p=preview (Watch the console)

ivarni
  • 17,658
  • 17
  • 76
  • 92
1

Worked for me the old way document.querySelector('a.btn.btn-info.showmore.ng-scope').click()

Rupesh
  • 2,627
  • 1
  • 28
  • 42
0

Yes you could, but you should assign an id:

<a id='Button'>SOMETEXT</a>

$("#Button").click(function() {
   alert('clicked');
   someAction();
);

This should fix it.

Max Langerak
  • 1,187
  • 8
  • 17