Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger ngClick programmatically

I want to trigger ng-click of an element at runtime like:

_ele.click(); 

OR

_ele.trigger('click', function()); 

How can this be done?

like image 215
mulla.azzi Avatar asked Mar 17 '14 05:03

mulla.azzi


People also ask

How to use ng-click in JavaScript?

Any field can be updated with ng-click using a custom JavaScript function. For this, we can make a clickable object in HTML (usually a button) and attach an ng-click directive with it that calls this custom function. The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked.


1 Answers

The syntax is the following:

function clickOnUpload() {   $timeout(function() {     angular.element('#myselector').triggerHandler('click');   }); };  // Using Angular Extend angular.extend($scope, {   clickOnUpload: clickOnUpload });  // OR Using scope directly $scope.clickOnUpload = clickOnUpload; 

More info on Angular Extend way here.

If you are using old versions of angular, you should use trigger instead of triggerHandler.

If you need to apply stop propagation you can use this method as follows:

<a id="myselector" ng-click="clickOnUpload(); $event.stopPropagation();">   Something </a> 
like image 133
clopez Avatar answered Sep 18 '22 12:09

clopez