使用angular.element通过$ ID获取范围对象 [英] Use angular.element to get scope object by $ID

查看:123
本文介绍了使用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.

推荐答案

您可以通过,请观察以下示例,尤其是对.scope()

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

这篇关于使用angular.element通过$ ID获取范围对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆