Debugging Made Easy with This New Feature in Angular 9 | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Debugging Made Easy with This New Feature in Angular 9

Debugging Made Easy with This New Feature in Angular 9

If debugging is the process of removing software bugs, then programming must be the process of putting them in.”

Edsger W. Dijkstra

The latest version Angular 9 includes a lot of major improvements, including the Ivy compiler and the reduction of bundle size using tree-shaking. You can explore the features of Angular 9 in this blog post.

The new Angular 9 enhances the way we debug and interact with components in development mode. With this update, you can:

  • Easily access the instance of components, pipes, or services.
  • Call methods, inspect any object values, or even change them manually with the help of instance.
  • Trigger the Angular change detection to reflect the changes in UI.

These capabilities are possible with the globally exposed ng object in the browser developer console.

Before going further, to more easily follow along with this blog post, we encourage you to create a new application using the latest version of Angular CLI.

In this blog, we are going to walk through some frequently required debugging options that are available by default in an Angular 9 CLI application.

Let’s dive in!

Getting the Angular global instance

The Angular team has exposed the global object ng, which is a lifesaver that makes the debugging process much easier. Follow these steps to get access to this global instance:

  1. Run the following command to launch the application locally.
    ng serve --open
    
  2. Open the browser developer console or press F12.
  3. In the Console tab, type ng and press Enter. All the default functions of the global ng object will be listed as shown in the following screenshot.

    The Global ng Object in the Developer Console
    The Global ng Object in the Developer Console

You can play with Angular components using the exposed functions. Now, let’s have a look at it in action with the default Angular CLI application.

Angular CLI Application
Angular CLI Application

Syncfusion Angular component suite is the only suite you will ever need to develop an Angular application faster.

Getting the Angular component instance

The method ng.getComponent is used to retrieve the component instance associated with the given DOM element. Follow these steps to get an underlying Angular component:

  1. Open the developer console and navigate to the Elements tab view.
  2. Select the element <app-root>, which will expose the $0 variable in the JavaScript console.
  3. Now, write the following line of code in the JavaScript console:
    ng.getComponent($0)
    

Now you can see the magic as shown in the following GIF.

Get the Angular Component in the Developer Console
Get the Angular Component in the Developer Console

Interacting with the Angular component

Now, the <app-root> Angular component instance is available along with all the variables, methods, and injected services, including the private methods and variables.

Interaction with the Angular component is easier using this instance. Run the following code to change the title of the component:

ng.getComponent($0).title = "Custom Title"

Unfortunately, no change can be seen in the UI part. This is because we didn’t notify Angular that the title of the component is changed from the JavaScript. Unless we notify the changes to Angular, they won’t be reflected in the UI.

A quick solution to overcome this is to trigger the Angular change detection manually. Angular has a method ng.applyChanges(component) to trigger the change detection.

ng.applyChanges($0)

Now you will see the title updated to Custom Title.

In a similar way, you can call any method exposed in the ng object. All the available functions in the ng object are listed in the table below, reproduced from the Angular documentation at @angular/core/global. You can refer to that documentation for further details.

ng.applyChangesMarks a component for check (in case of OnPush components) and synchronously performs change detection on the application this component belongs to.
ng.getComponentRetrieves the component instance associated with a given DOM element.
ng.getContextIf inside an embedded view (e.g., *ngIf or *ngFor), retrieves the context of the embedded view that the element is part of. Otherwise retrieves the instance of the component whose view owns the element (in this case, the result is the same as calling getOwningComponent).
ng.getDirectivesRetrieves directive instances associated with a given DOM element. Does not include component instances.
ng.getHostElementRetrieves the host element of a component or directive instance. The host element is the DOM element that matched the selector of the directive.
ng.getInjectorRetrieves an Injector associated with an element, component, or directive instance.
ng.getListenersRetrieves a list of event listeners associated with a DOM element. The list does include host listeners, but it does not include event listeners defined outside of the Angular context (e.g., through addEventListener).
ng.getOwningComponentRetrieves the component instance whose view contains the DOM element.
ng.getRootComponentsRetrieves all root components associated with a DOM element, directive, or component instance. Root components are those which have been bootstrapped by Angular.

Each and every property of Syncfusion Angular components are completely documented for easy access.

Interacting with Syncfusion Angular component

In this section, we are going to integrate the Syncfusion DatePicker Angular component with the Angular CLI application and interact with it.

  1. Follow the steps in the following user guide to integrate the Syncfusion DatePicker Angular component:
    Getting Started with Syncfusion Angular DatePicker Component

    Note: For simplicity, use the following code in the app.component.ts file:

    [app.component.ts]

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'angular-debugging';
      date: Date = new Date();
    }
    

    [app.component.html]
    Include the following code next to the highlighted card section.

    <!-- Highlight Card -->
      <div class="card highlight-card card-small">
     -------
     -------
     -------
      </div>
    
      <ejs-datepicker [value]='date' placeholder='Enter date'></ejs-datepicker>
    
  2. Now, the Angular CLI will look like the following screenshot.

    Syncfusion Angular DatePicker Component
    Syncfusion Angular DatePicker Component
  3. Now, access the DatePicker component and change the date value using the <app-root> element in the developer console. The following code example does this:
    ng.getComponent($0).date = new Date("1/1/2021")
    ng.applyChanges($0)
    

    Changing Date Value
    Changing Date Value

Note: Don’t forget to call the method ng.applyChanges(component) to see the changes in the UI.


Harness the power of Syncfusion’s feature-rich and powerful Angular UI components.

Conclusion

I hope you now have a clear idea how accessing the Angular global instance in the developer console makes debugging web applications easier.

Bonus round!

Attention Angular developers: Syncfusion offers over 65 high-performance, lightweight, modular, and responsive Angular components to speed up development.

For existing customers, the latest version of our controls is available for download from the License and Downloads page. If you are not yet a customer, you can try our 30-day free trial to check out all our Angular components have to offer. You can also explore samples in our GitHub repository.

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed