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

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


当前回答

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

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

其他回答

如果你已经安装了Batarang

然后你可以这样写:

$scope

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

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

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

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

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

只需在作用域外定义一个JavaScript变量,并将其分配到控制器中的作用域:

var myScope;
...
app.controller('myController', function ($scope,log) {
     myScope = $scope;
     ...

就是这样!它应该可以在所有浏览器中工作(至少在Chrome和Mozilla中测试)。

它正在工作,我正在用这个方法。

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

   window.scope = $scope;

  $scope.today = new Date();

我是这样使用它的。

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

为了改进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();