2014-09-20 79 views
3

我使用angulartics在我的項目,但是當我添加依賴我的模塊,我得到以下錯誤$位置:

未知提供商:$ rootElementProvider < - $ rootElement < - $ location。

我在HTML中插入angulartics.jsangular.js

它在.RUN生成後:LIB的

代碼在這裏:https://github.com/luisfarzati/angulartics/issues/203

$location好對象,但$rootElementProvider$rootElement是不確定的。

如何解決這個問題呢?

回答

0

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

乾杯!

+0

可能不是某些項目的最乾淨的方式,但它像一個魅力 – HeberLZ 2016-07-25 19:32:24

相關問題