我想访问我的$范围变量在Chrome的JavaScript控制台。我怎么做呢?
我既不能看到$scope也不能看到我的模块myapp的名称在控制台中作为变量。
我想访问我的$范围变量在Chrome的JavaScript控制台。我怎么做呢?
我既不能看到$scope也不能看到我的模块myapp的名称在控制台中作为变量。
当前回答
在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的控制台
其他回答
如果你已经安装了Batarang
然后你可以这样写:
$scope
当你在chrome的元素视图中选择了元素。 参考- https://github.com/angular/angularjs-batarang#console
只需将$scope赋值为全局变量。问题解决了。
app.controller('myCtrl', ['$scope', '$http', function($scope, $http) {
window.$scope = $scope;
}
实际上,我们在开发中比在生产中更需要$scope。
@JasonGoemaat已经提到了,但添加它作为这个问题的合适答案。
这也需要安装jQuery,但是非常适合开发环境。它查看每个元素以获得作用域的实例,然后返回带有控制器名称的实例。它还删除了任何以$开头的属性,这是angularjs通常用于配置的属性。
let controllers = (extensive = false) => {
let result = {};
$('*').each((i, e) => {
let scope = angular.element(e).scope();
if(Object.prototype.toString.call(scope) === '[object Object]' && e.hasAttribute('ng-controller')) {
let slimScope = {};
for(let key in scope) {
if(key.indexOf('$') !== 0 && key !== 'constructor' || extensive) {
slimScope[key] = scope[key];
}
}
result[$(e).attr('ng-controller')] = slimScope;
}
});
return result;
}
只需在作用域外定义一个JavaScript变量,并将其分配到控制器中的作用域:
var myScope;
...
app.controller('myController', function ($scope,log) {
myScope = $scope;
...
就是这样!它应该可以在所有浏览器中工作(至少在Chrome和Mozilla中测试)。
它正在工作,我正在用这个方法。
出于调试的目的,我把它放在控制器的开头。
window.scope = $scope;
$scope.today = new Date();
我是这样使用它的。
然后删除它,当我完成调试。