How do I access the $scope variable in browser's console using AngularJS?

前端 未结 19 1201
渐次进展
渐次进展 2020-11-22 08:35

I would like to access my $scope variable in Chrome\'s JavaScript console. How do I do that?

I can neither see $scope nor the name of my mo

相关标签:
19条回答
  • 2020-11-22 08:56

    I've used angular.element($(".ng-scope")).scope(); in the past and it works great. Only good if you have only one app scope on the page, or you can do something like:

    angular.element($("div[ng-controller=controllerName]")).scope(); or angular.element(document.getElementsByClassName("ng-scope")).scope();

    0 讨论(0)
  • 2020-11-22 08:59

    For only debugging purposes I put this to the start of the controller.

       window.scope = $scope;
    
      $scope.today = new Date();
    

    And this is how I use it.

    then delete it when I am done debugging.

    0 讨论(0)
  • 2020-11-22 09:01

    To add and enhance the other answers, in the console, enter $($0) to get the element. If it's an Angularjs application, a jQuery lite version is loaded by default.

    If you are not using jQuery, you can use angular.element($0) as in:

    angular.element($0).scope()
    

    To check if you have jQuery and the version, run this command in the console:

    $.fn.jquery
    

    If you have inspected an element, the currently selected element is available via the command line API reference $0. Both Firebug and Chrome have this reference.

    However, the Chrome developer tools will make available the last five elements (or heap objects) selected through the properties named $0, $1, $2, $3, $4 using these references. The most recently selected element or object can be referenced as $0, the second most recent as $1 and so on.

    Here is the Command Line API reference for Firebug that lists it's references.

    $($0).scope() will return the scope associated with the element. You can see its properties right away.

    Some other things that you can use are:

    • View an elements parent scope:

    $($0).scope().$parent.

    • You can chain this too:

    $($0).scope().$parent.$parent

    • You can look at the root scope:

    $($0).scope().$root

    • If you highlighted a directive with isolate scope, you can look at it with:

    $($0).isolateScope()

    See Tips and Tricks for Debugging Unfamiliar Angularjs Code for more details and examples.

    0 讨论(0)
  • 2020-11-22 09:02

    Just assign $scope as a global variable. Problem solved.

    app.controller('myCtrl', ['$scope', '$http', function($scope, $http) {
        window.$scope = $scope;
    }
    

    We actually need $scope more often in development than in production.

    Mentioned already by @JasonGoemaat but adding it as a suitable answer to this question.

    0 讨论(0)
  • 2020-11-22 09:03

    I usually use jQuery data() function for that:

    $($0).data().$scope
    

    The $0 is currently selected item in chrome DOM inspector. $1, $2 .. and so on are previously selected items.

    0 讨论(0)
  • 2020-11-22 09:04

    Somewhere in your controller (often the last line is a good place), put

    console.log($scope);
    

    If you want to see an inner/implicit scope, say inside an ng-repeat, something like this will work.

    <li ng-repeat="item in items">
       ...
       <a ng-click="showScope($event)">show scope</a>
    </li>
    

    Then in your controller

    function MyCtrl($scope) {
        ...
        $scope.showScope = function(e) {
            console.log(angular.element(e.srcElement).scope());
        }
    }
    

    Note that above we define the showScope() function in the parent scope, but that's okay... the child/inner/implicit scope can access that function, which then prints out the scope based on the event, and hence the scope associated with the element that fired the event.

    @jm-'s suggestion also works, but I don't think it works inside a jsFiddle. I get this error on jsFiddle inside Chrome:

    > angular.element($0).scope()
    ReferenceError: angular is not defined
    

    0 讨论(0)
提交回复
热议问题