我最近得到了同樣的問題,我知道這可能發生的唯一原因是當你手動創建一個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