34

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

}]);

5 Answers 5

33

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();
Sign up to request clarification or add additional context in comments.

8 Comments

what is "yourElement" - controller?
It's an element that is within the controllers scope, so <body ng-controller="yourController"><div id="someElem"></div></body> - the div with ID someElem is within the scope, so you can do: angular.element("someElem").scope() - see the guide for more: docs.angularjs.org/api/ng/function/angular.element
hm...my body has no ng-controller attribute. will the same rule apply when using ng-view?
Where is your controller defined?
in config, .when("/add", { controller: "AddCtrl", templateUrl: "views/add.html" })
|
25

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.

Comments

11

If you are only using angular and NOT using jquery you can do something like angular.element(document.getElementById('yourElement')).scope().save();

If you are using BOTH angular and jquery, you can do angular.element($('#yourElement')).scope().save(); assuming the the id attribute of your element is set like this <div id=yourElement></div>

VERY IMPORTANT But as noted here if the function you are calling from the console does anything such that you wish the changes to show up in the view then you have to pass them through $apply() like this

var scope = angular.element($('#story')).scope();
scope.$apply(function(){
  scope.showNextScene();
});

1 Comment

@legas Angular has silly quirks. Takes ages to get past them sometimes.
1

You can load a Chrome Extension for developers called AngularJS Batarang.

It will add an AngularJS tab to the developer console.

Selecting an element in your page, if it has a $scope, it will show in the "Elements" right hand pane, under the "$scope" tab.

1 Comment

As mentioned by others If jQuery is available, angular.element is just an alias for the jQuery function. If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or jqLite.
1
  1. In the Chrome inspector click on the element which is scope related to controller where you want to execute.
  2. Type angular.element($0).scope().nameYourFunctionInScope();
  3. Execute

2 Comments

Type it where ? ?
@LachezarRaychev in the console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.