How do I access the $scope variable in browser's console using AngularJS?

前端 未结 19 1619
渐次进展
渐次进展 2020-11-22 08:35

I would like to access my $scope variable in Chrome\'s JavaScript console. How do I do that?

I can neither see $scope nor the name of my mo

19条回答
  •  眼角桃花
    2020-11-22 08:54

    This requires jQuery to be installed as well, but works perfectly for a dev environment. It looks through each element to get the instances of the scopes then returns them labelled with there controller names. Its also removing any property start with a $ which is what angularjs generally uses for its configuration.

    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;
            }
    

提交回复
热议问题