Is it possible to call AngularJS controller function from console (Chrome Developer Tools console)?
e.g.
app.controller('AddCtrl', ['$scope', function ($scope) {
$scope.save = function(){
// do stuff here - call it from console
};
}]);
Calling a function or initializing a single value on page load in AngularJS is quite easy. AngularJS provides us with a dedicated directive for this specific task. It's the ng-init directive. Example 1: In this example, we will call a function to initialize a variable on page load.
You can call your angularJS function from javascript or jquery with the help of angular. element().
If you're using Chrome, we can use a shortcut with the developer tools. Simply find the element you're interested in, right click on it in the browser, and select inspect element. The element itself is stored as the $0 variable, and we can fetch the Angular-ized element by calling: angular. element($0) .
Definition and Usage. The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.
Yes, you just need to use angular.element
to get an element that's within the scope of your controller:
angular.element("yourElement").scope().save();
Open the browsers developer console. Inspect the element where the controller gets injected. Execute the following:
angular.element($0).scope().save()
$0 is the element you are currently selecting in the developer console elements panel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With