我最近得到了同样的问题,我知道这可能发生的唯一原因是当你手动创建一个angularjs注入器,这取决于在启动时注入$ location的模块,或者当你试图获得$通过该注射器定位自己。
这个问题实际上与angulartics库本身没有关系,而是与angular自己的$ location服务有关,它直接依赖于$ rootElement,它是在应用程序引导期间定义的元素,因此在应用程序启动之前不存在。
有一个简单的方法可以解决这个问题,如果你有angulartics这个问题,那就是从你的应用程序中删除angulartics作为依赖项,并将它作为resumeBootstrap方法的依赖项添加它,这允许我们添加更多依赖于运行时,同时恢复角度的引导过程。
例如:
angular.module('myApp', [
// array of dependencies without angulartics
]);
var preBootstrapInjector = angular.injector(['ng', 'myApp']);
var $rootScope = preBootstrapInjector.$get('$rootScope');
var myService = preBootstrapInjector.$get('myService');
myService.getDataFromServer()
.then(doSomethingWithThatData)
.then(resumeBootstrap);
function resumeBootstrap(){
// Clean up the custom injector for garbage collection
$rootScope.$destroy();
// Resume Angular's bootstrap process
$(document).ready(function() {
angular.resumeBootstrap([
// dependencies from modules that need $location
'angulartics'
]);
});
}
干杯!
可能不是某些项目的最干净的方式,但它像一个魅力 – HeberLZ 2016-07-25 19:32:24