最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use angular.element to get scope object by $ID - Stack Overflow

programmeradmin2浏览0评论

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:

$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 I could grab it using angular.element but I haven't had any successes.

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:

$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 I could grab it using angular.element but I haven't had any successes.

Share Improve this question edited Sep 16, 2015 at 19:33 scniro 17k8 gold badges66 silver badges107 bronze badges asked Sep 16, 2015 at 14:05 TheIntrepidSpiffTheIntrepidSpiff 1892 gold badges2 silver badges16 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 16

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


Update

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

发布评论

评论列表(0)

  1. 暂无评论