How to debug Angular JavaScript Code

前端 未结 13 1987
离开以前
离开以前 2020-12-04 06:48

I am working on a proof of concept using Angular JavaScript.

How to debug the Angular JavaScript code in different browsers (Firefox and Chrome) ?

相关标签:
13条回答
  • 2020-12-04 07:08
    var rootEle = document.querySelector("html");
    var ele = angular.element(rootEle); 
    

    scope() We can fetch the $scope from the element (or its parent) by using the scope() method on the element:

    var scope = ele.scope();
    

    injector()

    var injector = ele.injector();
    

    With this injector, we can then then instantiate any Angular object inside of our app, such as services, other controllers, or any other object

    0 讨论(0)
  • Add call to debugger where you intend to use it.

    someFunction(){
      debugger;
    }
    

    In the console tab of your browser's web developer tools, issue angular.reloadWithDebugInfo();

    Visit or reload the page you intend to debug and see the debugger appear in your browser.

    0 讨论(0)
  • 2020-12-04 07:13

    there is also $log that you can use! it makes use of your console in a way that you want it to work!

    showing the error/warning/info the way your console shows you normally!

    use this > Document

    0 讨论(0)
  • 2020-12-04 07:14

    Unfortunately most of add-ons and browser extensions are just showing the values to you but they don't let you to edit scope variables or run angular functions. If you wanna change the $scope variables in browser console (in all browsers) then you can use jquery. 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="name of your controller"]').scope()
    

    Example: You need to change value of $scope variable and see the result in the browser then just type in the browser console:

    angular.element('[ng-controller="mycontroller"]').scope().var1 = "New Value";
    angular.element('[ng-controller="mycontroller"]').scope().$apply();
    

    You can see the changes in your browser immediately. The reason we used $apply() is: any scope variable updated from outside angular context won't update it binding, You need to run digest cycle after updating values of scope using scope.$apply() .

    For observing a $scope variable value, you just need to call that variable.

    Example: You wanna see the value of $scope.var1 in the web console in Chrome or Firefox just type:

    angular.element('[ng-controller="mycontroller"]').scope().var1;
    

    The result will be shown in the console immediately.

    0 讨论(0)
  • 2020-12-04 07:15

    Since the add-ons don't work anymore, the most helpful set of tools I've found is using Visual Studio/IE because you can set breakpoints in your JS and inspect your data that way. Of course Chrome and Firefox have much better dev tools in general. Also, good ol' console.log() has been super helpful!

    0 讨论(0)
  • 2020-12-04 07:17

    For Visual Studio Code (Not Visual Studio) do Ctrl+Shift+P

    Type Debugger for Chrome in the search bar, install it and enable it.

    In your launch.json file add this config :

    {
        "version": "0.1.0",
        "configurations": [
            {
                "name": "Launch localhost with sourcemaps",
                "type": "chrome",
                "request": "launch",
                "url": "http://localhost/mypage.html",
                "webRoot": "${workspaceRoot}/app/files",
                "sourceMaps": true
            },
            {
                "name": "Launch index.html (without sourcemaps)",
                "type": "chrome",
                "request": "launch",
                "file": "${workspaceRoot}/index.html"
            },
        ]
    }
    

    You must launch Chrome with remote debugging enabled in order for the extension to attach to it.

    • Windows

    Right click the Chrome shortcut, and select properties In the "target" field, append --remote-debugging-port=9222 Or in a command prompt, execute /chrome.exe --remote-debugging-port=9222

    • OS X

    In a terminal, execute /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

    • Linux

    In a terminal, launch google-chrome --remote-debugging-port=9222

    Find More ===>

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