我想访问我的$范围变量在Chrome的JavaScript控制台。我怎么做呢?

我既不能看到$scope也不能看到我的模块myapp的名称在控制台中作为变量。


当前回答

只需将$scope赋值为全局变量。问题解决了。

app.controller('myCtrl', ['$scope', '$http', function($scope, $http) {
    window.$scope = $scope;
}

实际上,我们在开发中比在生产中更需要$scope。

@JasonGoemaat已经提到了,但添加它作为这个问题的合适答案。

其他回答

假设您想访问元素的作用域

<div ng-controller="hw"></div>

你可以在控制台中使用以下命令:

angular.element(document.querySelector('[ng-controller=hw]')).scope();

这将为您提供该元素的范围。

在Chrome的控制台:

 1. Select the **Elements** tab
 2. Select the element of your angular's scope. For instance, click on an element <ui-view>, or <div>, or etc.
 3. Type the command **angular.element($0).scope()** with following variable in the angular's scope

例子

angular.element($0).scope().a
angular.element($0).scope().b

Chrome的控制台

出于调试的目的,我把它放在控制器的开头。

   window.scope = $scope;

  $scope.today = new Date();

我是这样使用它的。

然后删除它,当我完成调试。

在angular中,我们通过angular.element()....获取jquery元素 让c…

.scope angular.element () ();

例子:

<div id=“”></div>

你可以先从DOM中选择一个在你想要检查的范围内的元素:

然后你可以通过在控制台中查询以下命令来查看作用域对象:

angular.element .scope (0) ()

你可以查询范围内的任何属性,例如:

.widgets angular.element(0)美元.scope ()

或者你可以检查附在范围上的控制器:

心理图像元素(0.06美元)。范围(myControllerName美元)。

(另一种可行的方法是在代码中添加断点。如果$scope目前在当前的“普通旧JavaScript”范围内,那么你可以在控制台中检查$scope的值。)