Debug AngularJS applications through terminals

Source: Internet
Author: User

Debug AngularJS applications through terminals

When we build AngularJS applications, it is always difficult to access the hidden data and services in applications through the JavaScript console of browsers such as Chrome, Firefox, and IE. The following are some simple tips to help us view or control the running Angular application through the Javascript console, so that the application can be easily tested and modified, even real-time modifications to our Angular application:

1: Access Scope

Access any scope or even isolated scope on the page through a simple JS program !) :

 
 
  1. > angular.element(targetNode).scope()  
  2. -> ChildScope {$id: "005", this: ChildScope, $$listeners: Object, $$listenerCount: Object, $parent: Scope…}  

For the isolation scope:

 
 
  1. > angular.element(targetNode).isolateScope()  
  2. -> Scope {$id: "009", $$childTail: ChildScope, $$childHead: ChildScope, $$prevSibling: ChildScope, $$nextSibling: Scope…} 

Here, 'targetnode' is used as an HTML node reference. You can easily create a 'targetnode' using 'document. querySelector'

2: view the scope tree

Sometimes, we need to view the scope level on the page to effectively debug our application. AngularJS Batarang is an extension of the Chrome browser we need. It can display the current scope level and has other very useful features.

3: capture any service

No matter where the ngApp is defined, we can use the injector function to capture references to any service. If the angular bootstrap method is used, we can manually capture $ rootElement ):

 
 
  1. > angular.element('html').injector().get('MyService')  
  2. -> Object {undo: function, redo: function, _pushAction: function, newDocument: function, init: function…} 

Then we can call the service, just as we can inject the service.

4: Access Controller instructions

Some commands define a controller with some additional features, usually shared. To access a controller instance with a given command from the console, you only need to use the controller () method:

 
 
  1. > angular.element('my-pages').controller()  
  2. -> Constructor {} 

The last approach is more advanced and not commonly used.

5: Chrome console features

The Chrome console has a bunch of good shortcuts to debug browser applications. This is the best practice for Angular development:

  • $0-$4: Access the 5 most recently selected DOM elements in the View window. It is very convenient to select the capture range.

  • $ (Selector) and $ (selector): A quick alternative to querySelector () and querySelectorAll.

Thanks @ zgohr for providing this method!

Conclusion

With a few simple tips, we can access the data in any scope on the page, view the scope hierarchy, inject services and control commands.

So next time, if you want to adjust it a little, check your work, or control AngularJS using the console, I hope you can remember these commands, and I think they are very practical!

Debugging AngularJS Apps from the Console

By http://www.oschina.net/translate/angularjs-console

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.