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

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


当前回答

我同意最好的是Batarang在选择对象后使用它的$scope(它与angular.element($0).scope()相同,甚至更短的jQuery: $($0).scope()(我的最爱))

同样,如果你像我一样在body元素上有主作用域,那么$('body').scope()就可以了。

其他回答

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

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

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

我使用了angular.element($(".ng-scope")).scope();在过去,它工作得很好。如果你在页面上只有一个应用范围,那就好了,或者你可以这样做:

angular.element ($ (" div [ng-controller = controllerName] ")) .scope ();或angular.element (document.getElementsByClassName(“ng-scope”).scope ();

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

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

angular.element .scope (0) ()

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

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

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

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

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

同样,我们可以像这样通过HTML元素的名称来访问作用域: angular.element (document.getElementsByName (onboardingForm) [0]) .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的控制台