如何从 angular 应用程序的控制台中捕获 $state 对象以进行调试?

How to capture the $state object in console from an angular application for debugging purpose?

我想在我的 angular 应用程序中执行 $state.reload() 以进行调试。请参阅此相关问题 how-do-i-reload-an-angular-partial-for-testing-purpose

但是,我无法在甚至没有公开 $rootScope 对象的应用程序中捕获 $state?我设法通过这样做获得了 $rootScope,如 how-to-access-update-rootscope-from-outside-angular

所示
var $body = angular.element(document.body);
var $rootScope = $body.scope().$root; 

然后如 Wiki

中所述
var myApp = angular.module('myApp', []);
angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
});

但是这并没有为 $rootScope.$state 设置任何内容,认为 $rootScope 现在可以访问了。有什么帮助吗?我正在使用 Angular 1.3.x.

编辑:即使这样也行不通,

angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
    console.log("Testing");
});

对于angular 1.5.x - 1.6.x

您需要安装这个 https://github.com/olov/ng-annotate,之后您的代码应该如下所示

angular.module("myApp").run(function ($rootScope, $state, $stateParams) {
    'ngInject'; // this will fix dependency injection
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
});

考虑到 AngularJS 应用程序是在 body 上启动的,应该可以使用

获取服务实例
angular.element(document.body).injector().get('$state');

无需将其暴露给范围。