{"id":234,"date":"2014-06-29T08:00:00","date_gmt":"2014-06-29T08:00:00","guid":{"rendered":"http:\/\/localhost\/?p=234"},"modified":"2015-04-02T13:23:20","modified_gmt":"2015-04-02T13:23:20","slug":"angularjs-console","status":"publish","type":"post","link":"https:\/\/ionic.io\/blog\/angularjs-console","title":{"rendered":"Debugging AngularJS Apps from the Console"},"content":{"rendered":"<p>When building AngularJS apps, it can be challenging to access data and services hidden deep in our application through the Javascript console in Chrome, Firefox, and IE. Here are some simple tricks we can use to inspect and control a running Angular app through the browser Javascript console, making it easy to test, modify, and even program our Angular app in real-time:<\/p>\n<p><!--more--><\/p>\n<h3 id=\"1accessscopes\">1: Access Scopes<\/h3>\n<p>We can access any scope (even isolated ones!) on the page with a simple JS one-liner:<\/p>\n<pre><code class=\"javascript\">&gt; angular.element(targetNode).scope()\n-&gt; ChildScope {$id: &quot;005&quot;, this: ChildScope, $$listeners: Object, $$listenerCount: Object, $parent: Scope\u2026}\n<\/code><\/pre>\n<p>Or for isolated scopes:<\/p>\n<pre><code class=\"javascript\">&gt; angular.element(targetNode).isolateScope()\n-&gt; Scope {$id: &quot;009&quot;, $$childTail: ChildScope, $$childHead: ChildScope, $$prevSibling: ChildScope, $$nextSibling: Scope\u2026}\n<\/code><\/pre>\n<p>Where <code>targetNode<\/code> is a reference to an HTML Node. You can grab one easily using <code>document.querySelector()<\/code>.<\/p>\n<h3 id=\"2inspectthescopetree\">2: Inspect the Scope Tree<\/h3>\n<p>Sometimes we need to see what the scopes look like on the page to effectively debug our app. The <a href=\"https:\/\/chrome.google.com\/webstore\/detail\/angularjs-batarang\/ighdmehidhipcmcojjgiloacoafjmpfk?hl=en\">AngularJS Batarang<\/a> is a Chrome extension that shows the live scope hierarchy and has some other really helpful features.<\/p>\n<p><img decoding=\"async\" data-src=\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\" alt=\"Batarang Scopes\" title=\"\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" class=\"lazyload\"><noscript><img decoding=\"async\" src=\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\" alt=\"Batarang Scopes\" title=\"\"><\/noscript><\/p>\n<h3 id=\"3grabanyservices\">3: Grab any Services<\/h3>\n<p>We can grab a reference to any service using the <code>injector<\/code> function of element where <code>ngApp<\/code> was defined, or indirectly though any element with the <code>ng-scope<\/code> class:<\/p>\n<pre><code class=\"javascript\">&gt; angular.element(document.querySelector(&#039;html&#039;)).injector().get(&#039;MyService&#039;)\n-&gt; Object {undo: function, redo: function, _pushAction: function, newDocument: function, init: function\u2026}\n\/\/ Or slightly more generic\n&gt; angular.element(document.querySelector(&#039;.ng-scope&#039;)).injector().get(&#039;MyService&#039;)\n<\/code><\/pre>\n<p>We can then call methods on that service just like we could if we injected it.<\/p>\n<h3 id=\"4accesscontrollerfordirective\">4: Access controller for directive<\/h3>\n<p>Some directives define a controller with certain additional (often shared) functionality. To access the instance of a controller for a given directive from the console, just use the <code>controller()<\/code> function:<\/p>\n<pre><code class=\"javascript\">&gt; angular.element(&#039;my-pages&#039;).controller()\n-&gt; Constructor {}\n<\/code><\/pre>\n<p>This last one is more advanced and not used as frequently.<\/p>\n<h3 id=\"5chromeconsolefeatures\">5: Chrome Console Features<\/h3>\n<p>Chrome has a ton of <a href=\"https:\/\/developer.chrome.com\/devtools\/docs\/commandline-api\">nice shortcuts<\/a> for debugging browser applications from the console. Here are some of the best ones for Angular development:<\/p>\n<ul>\n<li>\n<p><code>$0<\/code> &#8211; <code>$4<\/code>: Access the last 5 DOM elements selected in the inspector window. This is convenient for grabbing scopes for selected elements: <code>angular.element($0).scope()<\/code><\/p>\n<\/li>\n<li>\n<p><code>$(selector)<\/code> and <code>$$(selector)<\/code>: A quick replacement for <code>querySelector()<\/code> and <code>querySelectorAll<\/code>, respectively.<\/p>\n<\/li>\n<\/ul>\n<p>Thanks to <a href=\"http:\/\/twitter.com\/zgohr\">@zgohr<\/a> for this tip!<\/p>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>With a few simple tricks we can access the data for any scope on the page, inspect the scope hierarchy, inject services, and control directives.<\/p>\n<p>So the next time you want to make small tweaks, check your work, or control an AngularJS app from the console, I hope you&#8217;ll remember these commands and find them as useful as I do!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building AngularJS apps, it can be challenging to access data and services hidden deep in our application through the Javascript console in Chrome, Firefox, and IE. Here are some simple tricks we can use to inspect and control a running Angular app through the browser Javascript console, making it easy to test, modify, and [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"","discourse_permalink":"","wpdc_publishing_response":"","wpdc_publishing_error":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[2,3],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-all","tag-angularjs","tag-ionic"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.0 (Yoast SEO v23.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Debugging AngularJS Apps from the Console - Ionic Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ionic.io\/blog\/angularjs-console\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Debugging AngularJS Apps from the Console\" \/>\n<meta property=\"og:description\" content=\"When building AngularJS apps, it can be challenging to access data and services hidden deep in our application through the Javascript console in Chrome, Firefox, and IE. Here are some simple tricks we can use to inspect and control a running Angular app through the browser Javascript console, making it easy to test, modify, and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ionic.io\/blog\/angularjs-console\" \/>\n<meta property=\"og:site_name\" content=\"Ionic Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-29T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-04-02T13:23:20+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\" \/>\n<meta name=\"author\" content=\"Max Lynch\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@maxlynch\" \/>\n<meta name=\"twitter:site\" content=\"@ionicframework\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Max Lynch\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#article\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console\"},\"author\":{\"name\":\"Max Lynch\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e\"},\"headline\":\"Debugging AngularJS Apps from the Console\",\"datePublished\":\"2014-06-29T08:00:00+00:00\",\"dateModified\":\"2015-04-02T13:23:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console\"},\"wordCount\":355,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage\"},\"thumbnailUrl\":\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\",\"keywords\":[\"angularjs\",\"Ionic\"],\"articleSection\":[\"All\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ionic.io\/blog\/angularjs-console#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console\",\"url\":\"https:\/\/ionic.io\/blog\/angularjs-console\",\"name\":\"Debugging AngularJS Apps from the Console - Ionic Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage\"},\"thumbnailUrl\":\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\",\"datePublished\":\"2014-06-29T08:00:00+00:00\",\"dateModified\":\"2015-04-02T13:23:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ionic.io\/blog\/angularjs-console\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage\",\"url\":\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\",\"contentUrl\":\"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ionic.io\/blog\/angularjs-console#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ionic.io\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Debugging AngularJS Apps from the Console\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ionic.io\/blog\/#website\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"name\":\"ionic.io\/blog\",\"description\":\"Build amazing native and progressive web apps with the web\",\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ionic.io\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ionic.io\/blog\/#organization\",\"name\":\"Ionic\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"width\":1920,\"height\":854,\"caption\":\"Ionic\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/ionicframework\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e\",\"name\":\"Max Lynch\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg\",\"caption\":\"Max Lynch\"},\"description\":\"CEO\",\"sameAs\":[\"http:\/\/twitter.com\/maxlynch\",\"https:\/\/x.com\/maxlynch\"],\"url\":\"https:\/\/ionic.io\/blog\/author\/max\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Debugging AngularJS Apps from the Console - Ionic Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ionic.io\/blog\/angularjs-console","og_locale":"en_US","og_type":"article","og_title":"Debugging AngularJS Apps from the Console","og_description":"When building AngularJS apps, it can be challenging to access data and services hidden deep in our application through the Javascript console in Chrome, Firefox, and IE. Here are some simple tricks we can use to inspect and control a running Angular app through the browser Javascript console, making it easy to test, modify, and [&hellip;]","og_url":"https:\/\/ionic.io\/blog\/angularjs-console","og_site_name":"Ionic Blog","article_published_time":"2014-06-29T08:00:00+00:00","article_modified_time":"2015-04-02T13:23:20+00:00","og_image":[{"url":"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png"}],"author":"Max Lynch","twitter_card":"summary_large_image","twitter_creator":"@maxlynch","twitter_site":"@ionicframework","twitter_misc":{"Written by":"Max Lynch","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ionic.io\/blog\/angularjs-console#article","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console"},"author":{"name":"Max Lynch","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e"},"headline":"Debugging AngularJS Apps from the Console","datePublished":"2014-06-29T08:00:00+00:00","dateModified":"2015-04-02T13:23:20+00:00","mainEntityOfPage":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console"},"wordCount":355,"commentCount":6,"publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"image":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage"},"thumbnailUrl":"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png","keywords":["angularjs","Ionic"],"articleSection":["All"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ionic.io\/blog\/angularjs-console#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ionic.io\/blog\/angularjs-console","url":"https:\/\/ionic.io\/blog\/angularjs-console","name":"Debugging AngularJS Apps from the Console - Ionic Blog","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage"},"image":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage"},"thumbnailUrl":"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png","datePublished":"2014-06-29T08:00:00+00:00","dateModified":"2015-04-02T13:23:20+00:00","breadcrumb":{"@id":"https:\/\/ionic.io\/blog\/angularjs-console#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ionic.io\/blog\/angularjs-console"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/angularjs-console#primaryimage","url":"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png","contentUrl":"http:\/\/ionicframework.com.s3.amazonaws.com\/blog\/angular-batarang.png"},{"@type":"BreadcrumbList","@id":"https:\/\/ionic.io\/blog\/angularjs-console#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ionic.io\/blog"},{"@type":"ListItem","position":2,"name":"Debugging AngularJS Apps from the Console"}]},{"@type":"WebSite","@id":"https:\/\/ionic.io\/blog\/#website","url":"https:\/\/ionic.io\/blog\/","name":"ionic.io\/blog","description":"Build amazing native and progressive web apps with the web","publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ionic.io\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ionic.io\/blog\/#organization","name":"Ionic","url":"https:\/\/ionic.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","width":1920,"height":854,"caption":"Ionic"},"image":{"@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/ionicframework"]},{"@type":"Person","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e","name":"Max Lynch","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg","caption":"Max Lynch"},"description":"CEO","sameAs":["http:\/\/twitter.com\/maxlynch","https:\/\/x.com\/maxlynch"],"url":"https:\/\/ionic.io\/blog\/author\/max"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}