How to use AngularJS access $ scope variables in the browser console?

本文翻译自:How do I access the $scope variable in browser's console using AngularJS?

Would like My Access to the I $scopevariable in Chrome's JavaScript Console. I want to access in Chrome's JavaScript console $scopevariables. How do I do that? How do I do?

See CAN neither the I $scopeNOR at The name of My Module myappin the Variables at The Console AS. I do not see either $scopecould not see my console module myappname as a variable.


#1st Floor

Reference: How https://stackoom.com/question/vfCE/ use AngularJS access -scope variable in the browser console


#2nd Floor

Pick an element in the HTML panel of the developer tools and type this in the console: Select an element in the HTML Developer Tools panel, and then enter the following elements in the console:

angular.element($0).scope()

An In WebKit and Firefox, $0IS A Reference to the Selected at The DOM the Node at The elements in the Tab, by doing the this SO you the Selected at The DOM GET OUT in the Node scope Printed at The Console. In WebKit and Firefox, $0is selected DOM elements tab reference nodes. Therefore, by performing this operation, you can print out the selected range DOM node in the console.

You can also target the scope by element ID, like so: You can also press element ID targeting scope, as follows:

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

Addons / Extensions plugin / extension

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

  • Batarang . Bata Lang This has been around for a while. It's been a while.

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

Playing with jsFiddle 玩jsFiddle

Working with CAN jsFiddle you the when Open at The fiddle in Show the MODE by Adding /showAT at The URL at The End of. When using jsfiddle, you can display by adding at the end of the URL mode /showto open the violin. Running like the this you have have the when to Access at The angular, Ltd. Free Join. Like running, you can access angularglobal. You can try it here: You can try it here:

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

jQuery Lite jQuery Lite

The Load jQuery you the before AngularJS IF, angular.elementCAN BE A jQuery Selector passed. If you load jQuery before AngularJS, angular.elementit may be to angular.elementpass jQuery selector. So you could inspect the scope of a controller with a result, you can use the following command to check the controller range

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

Of a button a button

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

And SO ON .... ... and so on.

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

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

Now you could do this now, you can do this

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

Here Wallpaper the Check: http://jsfiddle.net/jaimem/DvRaR/1/show/ checked here: HTTP : //jsfiddle.net/jaimem/DvRaR/1/show/


#3rd floor

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

console.log($scope);

If you want to see an inner / implicit scope, say inside an ng-repeat, something like this will work. To view the internal / implicit scope, such as in the ng-repeat, can be used similarly.

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

Then in your controller and 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. Please note that the above we define showScope () function in the parent scope, but it does not matter ... the child / internal / implicit scopes can access the function, then that function according to the event Print scope, thus triggering element associated with the scope of the event.

Also the Suggestion-JM's @ Works, But the I do not of Think IT Works Inside A jsFiddle. recommend @ jm- can be, but I think it does not work inside jsFiddle. I get this error on jsFiddle inside Chrome : I received this error on jsFiddle in Chrome:

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


#4th floor

To improve on jm's answer ... in order to improve jm answer ...

// Access whole scope
angular.element(myDomElement).scope();

// Access and change variable in scope
angular.element(myDomElement).scope().myVar = 5;
angular.element(myDomElement).scope().myArray.push(newItem);

// Update page to reflect changed variables
angular.element(myDomElement).scope().$apply();

Or if you're using jQuery, this does the same thing ... or if you're using jQuery, you can do the same thing ...

$('#elementId').scope();
$('#elementId').scope().$apply();

A Way to Easy Access Another DOM Element from The Console (jm Mentioned AS) IS IT to ON in the Click The 'Elements' Tab, and the gets Stored Automatically IT AS $0. Another simple way to access the DOM element from the console (e.g., jm a) by clicking it in the "elements" tab, it is automatically stored as $0.

angular.element($0).scope();

#5th Floor

You have have Installed IF Batarang If you have already installed Batarang

Then you can just write: Then you can write:

$scope

when you have the element selected in the elements view in chrome. When you select an element in Chrome elements in view. Ref - https://github.com/angular/angularjs-batarang#console reference -https: //github.com/angular/angularjs-batarang#console


#6th floor

The I Agree at The Best IS batarang with IT's $scopethe After the Selecting AN Object (IT's at The Same, AS angular.element($0).scope()or the even Shorter with jQuery: $($0).scope()(My Favorite)) I agree that the best is to select the object angular.element($0).scope()of $scope(and angular.element($0).scope()or even shorter than jQuery: $($0).scope()(I most like))

Also, IF you have have you like Me at The main scope ON bodyElement, A $('body').scope()Works Fine. In addition, if, like me, you will be subject range is set in bodythe elements, it $('body').scope()can work properly.

Original articles published 0 · won praise 136 · views 830 000 +

Guess you like

Origin blog.csdn.net/xfxf996/article/details/105250115