使用angular.element通过$ ID获取范围对象
我需要将数据从angular应用程序传递到在angular之外运行的脚本,因为我无权编辑angular应用程序的代码.
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.
使用适用于Chrome的Angular Batarang和NG-Inspector扩展,我可以看到需要从中提取的JSON对象,但我对如何开始一无所知.
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.
例如,在Angular Batarang中,对象看起来像:
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
我最初的想法是我可以使用angular.element
来抓住它,但是我没有取得任何成功.
My initial thought was I could grab it using angular.element
but I haven't had any successes.
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示例-演示
更新
根据 docs ,必须启用调试模式
Per the docs, debug mode must be enabled.
scope()-检索当前元素或其父元素的范围.需要启用调试数据.
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