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

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


当前回答

为了改进jm的回答……

// Access whole scope
angular.element(myDomElement).scope();

// Access and change variable in scope
angular.element(myDomElement).scope().myVar = 5;
angular.element(myDomElement).scope().myArray.push(newItem);

// Update page to reflect changed variables
angular.element(myDomElement).scope().$apply();

或者如果你使用jQuery,它也会做同样的事情…

$('#elementId').scope();
$('#elementId').scope().$apply();

从控制台访问DOM元素的另一种简单方法(正如jm提到的)是在“elements”选项卡中单击它,它自动被存储为$0。

angular.element($0).scope();

其他回答

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

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

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

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

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

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

.scope angular.element () ();

例子:

<div id=“”></div>

我通常使用jQuery data()函数:

$($0).data().$scope

$0是当前在chrome DOM检查器中选中的项目。 1美元,2美元…等等都是之前选定的项目。

如果你已经安装了Batarang

然后你可以这样写:

$scope

当你在chrome的元素视图中选择了元素。 参考- https://github.com/angular/angularjs-batarang#console

在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的控制台