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

前端 未结 19 1717
渐次进展
渐次进展 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 09:04

    Somewhere in your controller (often the last line is a good place), put

    console.log($scope);
    

    If you want to see an inner/implicit scope, say inside an ng-repeat, something like this will work.

  • ... show scope
  • Then in your controller

    function MyCtrl($scope) {
        ...
        $scope.showScope = function(e) {
            console.log(angular.element(e.srcElement).scope());
        }
    }
    

    Note that above we define the showScope() function in the parent scope, but that's okay... the child/inner/implicit scope can access that function, which then prints out the scope based on the event, and hence the scope associated with the element that fired the event.

    @jm-'s suggestion also works, but I don't think it works inside a jsFiddle. I get this error on jsFiddle inside Chrome:

    > angular.element($0).scope()
    ReferenceError: angular is not defined
    

提交回复
热议问题