{"id":824,"date":"2023-04-19T05:00:00","date_gmt":"2023-04-19T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=824"},"modified":"2024-10-21T12:44:25","modified_gmt":"2024-10-21T19:44:25","slug":"debugging-with-the-ng-namespace","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/debugging-with-the-ng-namespace\/","title":{"rendered":"Debugging with the ng namespace"},"content":{"rendered":"\n<p>Angular 9 introduced the <code>ng<\/code> namespace variable, which is available in development mode but not exposed when the application is running in production mode.<\/p>\n\n\n\n<p>We can debug an application using that global namespace without any <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/the-debugger-keyword\/\" target=\"_blank\" rel=\"noopener\" title=\"\">breakpoint<\/a> or <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-devtools-extension\/\" target=\"_blank\" rel=\"noopener\" title=\"\">dev tools extension<\/a>. Here&#8217;s how.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">First, select a DOM element<\/h2>\n\n\n\n<p>Using your browser dev tools, select a DOM element on the page as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"399\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/1_xsERiXMDj614KrX5IKKtlw.gif\" alt=\"\" class=\"wp-image-826\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>When an element is selected using the above process, it becomes assigned to a variable called <code>$0<\/code> in the developer tools console. The previous selection is known as <code>$1<\/code>, the one before that <code>$2<\/code>, and on and on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Then, use the <code>ng<\/code> workspace functions<\/h2>\n\n\n\n<p>If the selected DOM element <strong>is an Angular component<\/strong>, you can head to the browser console and type <code>ng.getComponent($0)<\/code>. This returns a reference to the component instance &#8211; ready to be explored and debugged:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"373\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-33.png\" alt=\"\" class=\"wp-image-827\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-33.png 602w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-33-300x186.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If the element you select is not a component (say a <code>div<\/code> or an image), use <code>ng.getOwningComponent($0)<\/code> to get a reference to the parent component of the selected DOM element.<\/p>\n\n\n\n<p>If you want to select the directives applied to that element, use <code>ng.getDirectives($0)<\/code>. It&#8217;s also possible to <a href=\"https:\/\/blog.angulartraining.com\/how-to-debug-angular-applications-5fcc98900c52#:~:text=What%20if%20I,variables%20and%20methods%3A\" target=\"_blank\" rel=\"noopener\" title=\"\">access the context of a template in a ngFor or ngIf directive<\/a>.<\/p>\n\n\n\n<p>There are a few more useful <a href=\"https:\/\/angular.io\/api\/core\/global\" target=\"_blank\" rel=\"noopener\" title=\"\">functions available in that global namespace<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular 9 introduced the ng namespace variable, which is available in development mode but not exposed when the application is running in production mode. We can debug an application using that global namespace without any breakpoint or dev tools extension. Here&#8217;s how. First, select a DOM element Using your browser dev tools, select a DOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-angular","category-debugging"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/824\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}