Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Angular Controller Function from Browser Console

Tags:

angularjs

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
    };

}]);
like image 565
Iladarsda Avatar asked Apr 01 '14 19:04

Iladarsda


People also ask

Can we call function in angular?

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.

How do you call a function in AngularJS?

You can call your angularJS function from javascript or jquery with the help of angular. element().

How do I inspect AngularJS in Chrome?

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) .

What is Ng-controller in HTML?

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.


2 Answers

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();
like image 134
tymeJV Avatar answered Oct 18 '22 12:10

tymeJV


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.

like image 25
GonchuB Avatar answered Oct 18 '22 12:10

GonchuB