Use angular.element to get scope object by $ID

All we need is an easy explanation of the problem, so here it is.

I need to pass data from an angular app to scripts that run outside of angular because I do not have access to editing the code of the angular app.

Using Angular Batarang and NG-Inspector extensions for Chrome, I can see the JSON object I need to pull from, but I am at a loss of how to start.

For instance, in Angular Batarang, the object looks like:

name: "listing"
   0: "alpha"
   1: "beta"
   user: "test"
   userID: "12345"
   address: "555 Elm St"
   phone: 555.555.5555

My initial thought was I could grab it using angular.element but I haven’t had any successes.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

you can determine which element that scope is bound to, select the element, and grab it’s scope via angular.element. Assume this scope is attached to element <div id="stuff"></div>, observe the following example, specifically, the call to .scope()

<div ng-app="app" ng-controller="ctrl" id="stuff"></div>

<button onclick="getStuff()">get stuff</button>

var app = angular.module('app', []).controller('ctrl', function($scope) {
   $scope.inside = { 'name': 'guy', 'idk': 'blah' }

var getStuff = function() {
    var outside = angular.element(document.getElementById('stuff')).scope();
    console.log(outside.inside) // retrieved "outside" of AngularJS

JSFiddle Example – demo


Per the docs, debug mode must be enabled.

scope() – retrieves the scope of the current element or its parent. Requires Debug Data to be enabled.

It’s enabled by default, and disabling it will cause issue here

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply