Some text and I want to ...">

How to trigger emulate ngClick click from jQuery

How is the link, for example

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

and I want to call the ngClick action from jQuery:

 $('a').click() 

But this does not work: someAction() not called. Also did not work

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

Is it possible to call someAction() from jQuery?

+6
source share
3 answers

Do not pay attention to the applied cycles and timeouts, here you are:

 $('a').dispatchEvent(new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true })); 
+3
source

It seems strange to me that it doesn’t work, I get his work with both

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

and

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

as selectors and both click() and trigger('click') to trigger the clicker.

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

+4
source

Yes you could, but you have to assign id:

 <a id='Button'>SOMETEXT</a> $("#Button").click(function() { alert('clicked'); someAction(); ); 

This should fix it.

+1
source

All Articles