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

前端 未结 19 1235
渐次进展
渐次进展 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:41

    At the Chrome's console :

     1. Select the **Elements** tab
     2. Select the element of your angular's scope. For instance, click on an element <ui-view>, or <div>, or etc.
     3. Type the command **angular.element($0).scope()** with following variable in the angular's scope
    

    Example

    angular.element($0).scope().a
    angular.element($0).scope().b
    

    Chrome's console

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

    I agree the best is Batarang with it's $scope after selecting an object (it's the same as angular.element($0).scope() or even shorter with jQuery: $($0).scope() (my favorite))

    Also, if like me you have you main scope on the body element, a $('body').scope() works fine.

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

    Just define a JavaScript variable outside the scope and assign it to your scope in your controller:

    var myScope;
    ...
    app.controller('myController', function ($scope,log) {
         myScope = $scope;
         ...
    

    That's it! It should work in all browsers (tested at least in Chrome and Mozilla).

    It is working, and I'm using this method.

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

    Pick an element in the HTML panel of the developer tools and type this in the console:

    angular.element($0).scope()
    

    In WebKit and Firefox, $0 is a reference to the selected DOM node in the elements tab, so by doing this you get the selected DOM node scope printed out in the console.

    You can also target the scope by element ID, like so:

    angular.element(document.getElementById('yourElementId')).scope()
    

    Addons/Extensions

    There are some very useful Chrome extensions that you might want to check out:

    • Batarang. This has been around for a while.

    • ng-inspector. This is the newest one, and as the name suggests, it allows you to inspect your application's scopes.

    Playing with jsFiddle

    When working with jsfiddle you can open the fiddle in show mode by adding /show at the end of the URL. When running like this you have access to the angular global. You can try it here:

    http://jsfiddle.net/jaimem/Yatbt/show

    jQuery Lite

    If you load jQuery before AngularJS, angular.element can be passed a jQuery selector. So you could inspect the scope of a controller with

    angular.element('[ng-controller=ctrl]').scope()
    

    Of a button

     angular.element('button:eq(1)').scope()
    

    ... and so on.

    You might actually want to use a global function to make it easier:

    window.SC = function(selector){
        return angular.element(selector).scope();
    };
    

    Now you could do this

    SC('button:eq(10)')
    SC('button:eq(10)').row   // -> value of scope.row
    

    Check here: http://jsfiddle.net/jaimem/DvRaR/1/show/

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

    Say you want to access the scope of the element like

    <div ng-controller="hw"></div>
    

    You could use the following in the console:

    angular.element(document.querySelector('[ng-controller=hw]')).scope();
    

    This will give you the scope at that element.

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

    This is a way of getting at scope without Batarang, you can do:

    var scope = angular.element('#selectorId').scope();
    

    Or if you want to find your scope by controller name, do this:

    var scope = angular.element('[ng-controller=myController]').scope();
    

    After you make changes to your model, you'll need to apply the changes to the DOM by calling:

    scope.$apply();
    
    0 讨论(0)
提交回复
热议问题