Use angular.element to get the area object by $ ID

I need to pass data from an angular application into scripts that work outside of angular because I don't have access to edit the angular application code.

Using the angular extensions of Batarang and NG-Inspector for Chrome, I see a JSON object from which I need to extract, but I can not start from.

For example, in angular Batarang, an object looks like this:

$id=5 name: "listing" keys: 0: "alpha" 1: "beta" alpha: user: "test" userID: "12345" beta: address: "555 Elm St" phone: 555.555.5555 

My initial thought was that I could grab it with angular.element , but I had no success.

+5
source share
1 answer

you can determine which element the area is attached to, select the element and capture it using angular.element . Assuming that this area is attached to the <div id="stuff"></div> element, pay attention to the following example, in particular, calling .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

+11
source

All Articles