Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger emulate click ngClick from jQuery

Tags:

angularjs

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?

like image 921
uladzimir Avatar asked Apr 01 '14 05:04

uladzimir


2 Answers

Nevermind the apply cycles and timeouts, here you are:

$('a').dispatchEvent(new MouseEvent('click', {
 'view': window,
 'bubbles': true,
 'cancelable': true
}));
like image 156
Vlad Gurovich Avatar answered Dec 12 '22 15:12

Vlad Gurovich


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)

like image 28
ivarni Avatar answered Dec 12 '22 15:12

ivarni