Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug objects from Angular template (html file)

Creating a template, I have some Angular code within some HTML elements:

<button id="btnMainMenu" class="button button-icon fa fa-chevron-left header-icon"         ng-if="(!CoursesVm.showcheckboxes || (CoursesVm.tabSelected == 'current') )" ... 

I want to debug the ng-if condition to check the values of my CoursesVm object. How would I do this in Chrome for example?

like image 944
Chicowitz Avatar asked Mar 30 '17 19:03

Chicowitz


People also ask

How do I debug Angular HTML in Chrome?

After editing your default code of App component, serve your app with the following Angular CLI command. This will be your output after changes. Now we need a Google chrome debugger extension for Visual Studio code, so click on extension option at left side icon of VS code.

How do I debug a HTML file?

Debugging with the Google Chrome Browser The Sources panel is where you debug JavaScript. To open DevTools, press Command + Option + I (Mac), Control + Shift + I (Windows, Linux), or F12. This shortcut opens the Console panel. We will look at the debugger in more detail in the Java Script chapter.


2 Answers

For people looking for Angular (2+), use json pipe

for eg:

 <span>{{ CoursesVm | json }}</span>   <textarea>{{ CoursesVm | json }}</textarea> 
like image 140
santon Avatar answered Oct 02 '22 20:10

santon


Option 1: Modify your code (For Angular2+ and AngularJS)

Angular2+

...in the component add this temporal function

checkIf(value: any){     debugger;  //open the devtools and go to the view...code execution will stop here!     //..code to be checked... `value` can be inspected now along with all of the other component attributes } 

... in the view: add an *ngIf with the created function providing the value you want to debug

<button *ngIf="checkIf(CoursesVm)">Button</button> 

AngularJS

You can enclose the code inside the ng-if ((!CoursesVm.showcheckboxes || (CoursesVm.tabSelected == 'current') )) inside a controller function and then debug that function.

Something like this:

//...controller function checkIf(){     debugger;  //open the devtools and go to the view...code execution will stop here!     //..code to be checked }   <!--view supposing myCtrl is the alias for the controller here--> <button id="btnMainMenu" class="button button-icon fa fa-chevron-left header-icon"         ng-if="myCtrl.checkIf()" <!-- ... --> 

Option 2: Directly in chrome devtools (For AngularJS (Known to some people as Angular 1))

  • Capture the scope like this:

    var scope = angular.element(document.getElementById('#btnMainMenu')).scope();

  • Access to the object like this (supposing the controller of this view is myCtrl):

scope.myCtrl.CoursesVm

like image 25
lealceldeiro Avatar answered Oct 02 '22 21:10

lealceldeiro