我遵循John Papa的角度樣式指南(https://github.com/johnpapa/angular-styleguide#routing)並在本指南中提供的角度ui路由器周圍使用自定義封裝。然而,包裝不爲我工作,並注入$狀態時,我得到一個循環依賴錯誤:ui路由器封裝中的Angular.js循環依賴錯誤
Uncaught Error: [$injector:cdep] Circular dependency found: $rootScope <- $timeout <- $$rAF <- $$animateQueue <- $animate <- toastr <- logger <- $exceptionHandler <- $rootScope <- $state <- routerHelper
我曾嘗試手動進$狀態使用$注射器但是這給了我一個未知的提供程序錯誤。
下面是代碼:
(function() {
'use strict';
angular
.module('blocks.router')
.provider('routerHelper', routerHelperProvider);
routerHelperProvider.$inject = ['$locationProvider', '$stateProvider', '$urlRouterProvider', '$injector'];
function routerHelperProvider($locationProvider, $stateProvider, $urlRouterProvider) {
this.$get = RouterHelper;
$locationProvider.html5Mode(true);
RouterHelper.$inject = ['$state'];
function RouterHelper($state) {
var hasOtherwise = false;
var service = {
configureStates: configureStates,
getStates: getStates
};
return service;
function configureStates(states, otherwisePath) {
states.forEach(function (state) {
$stateProvider.state(state.state, state.config);
});
if (otherwisePath && !hasOtherwise) {
hasOtherwise = true;
$urlRouterProvider.otherwise(otherwisePath);
}
}
function getStates() {
return $state.get();
}
}
}
})();
嘗試在HotTowel模板上使用angular-toastr時遇到同樣的問題。使用$ inject延遲依賴關係爲我工作。正如@Casey所說,不確定這是否是正確的方法。 – JenonD