2015-11-17 70 views
0

我在过去使用过AngularJS做过几个项目。在开发过程中,我总是可以在浏览器控制台中调用$ scope,从而调用它的所有变量。例如,$scope.myArray会返回类似[][object1, object2]

但是,现在当我在$scope型,我得到:

Uncaught ReferenceError: $scope is not defined

我也看了其它的帖子像How to access the $scope variable in browser's console using AngularJS?,并建议使用angular.element($0).scope(),但是当我键入控制台,我得到:

undefined

,我不能像myArray访问的对象,即使我知道正在创建它。有谁知道改变了什么?

编辑:我也遇到这个问题,当我尝试从以前的项目中,我曾经能够访问$ scope的代码,所以这让我觉得这不是一个简单的实现错误。

+0

入住这https://docs.angularjs.org/guide/production#disabling-debug-data – estus

+0

它仍然工程与依赖注入,这是在扮演 – dandavis

+0

任何地方“$ 0”很可能不是指适当的元素。找到元素的选择器(例如,通过检查元素,在“元素”面板中右键单击它并单击“获取CSS路径”),并执行angular.element(theCssPath).scope()。 –

回答

0

内的控制器的定义把window.myScope = $scope。然后在控制台中,您可以拨打myScope.myArray让事情返回给您。

例如:

var app = angular.module('myApp', []) 

app.controller('myCtrl', function($scope) { 
window.myScope = $scope; //this allows console access 

$scope.myArray = []; 
//more code here 
}); 
0

$ 0是元素选项卡中对所选DOM节点的引用,因此通过执行此操作可以在控制台中打印出所选DOM节点作用域。

您必须先获取元素。 或者您可以只获取指定的范围。 例如:

angular.element('‪#‎element‬').scope(); 

angular.element(document.getElementsByTagName("button")[0]).scope() 

https://jsfiddle.net/fatb8g3z/

+0

我仍然无法得到这个工作。我键入'angular.element(“button”)。scope();'得到这个:'未捕获的错误:[jqLit​​e:nosel] http:// errors.angularjs.org/1.3.14/jqLit​​e/nosel' – reubonwry

+0

这是jqLit​​e的问题。尝试这个。 (document.getElementsByTagName(“button”)[0])。scope() – augustine

相关问题