Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to run the ng-click function through the console in a few different ways:

  1. Select the element with the ng-click directive in the console and trigger the click event manually:
var element = document.querySelector('[ng-click="myFunction()"]');
angular.element(element).triggerHandler('click');
  1. Use the AngularJS injector to get the controller function and call it with the $scope variable:
var injector = angular.element(document.querySelector('[ng-app]')).injector();
var $rootScope = injector.get('$rootScope');
var $controller = injector.get('$controller');
var $scope = $rootScope.$new();
$scope.myFunction(); // call the function directly
  1. Use the AngularJS $apply function to trigger the ng-click event:
var element = document.querySelector('[ng-click="myFunction()"]');
var $scope = angular.element(element).scope();
$scope.$apply(function() { $scope.myFunction(); });