Ask Your Question
2

Is it possible to run the ng-click function through the console?

asked 2023-06-17 10:20:30 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-17 10:40:01 +0000

devzero gravatar image

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(); });
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-17 10:20:30 +0000

Seen: 8 times

Last updated: Jun 17 '23