I'd like to implement a setup where i can define a "root state" in the main module, and then add child states in other modules. This, because i need the root state to resolve before i can go to the child state.
Apparently, this should be possible according to this FAQ: How to: Configure ui-router from multiple modules
For me it doesn't work: Error Uncaught Error: No such state 'app' from ngBoilerplate.foo
Here is what i have:
app.js
angular.module( 'ngBoilerplate', [
'templates-app',
'templates-common',
'ui.state',
'ui.route',
'ui.bootstrap',
'ngBoilerplate.library'
])
.config( function myAppConfig ( $stateProvider, $urlRouterProvider ) {
$stateProvider
.state('app', {
views:{
"main":{
controller:"AppCtrl"
}
},
resolve:{
Auth:function(Auth){
return new Auth();
}
}
});
$urlRouterProvider.when('/foo','/foo/tile');
$urlRouterProvider.otherwise( '/foo' );
})
.factory('Auth', ['$timeout','$q', function ($timeout,$q) {
return function () {
var deferred = $q.defer();
console.log('before resolve');
$timeout(function () {
console.log('at resolve');
deferred.resolve();
}, 2000);
return deferred.promise;
};
}])
.run(function run( $rootScope, $state, $stateParams ) {
console.log('greetings from run');
$state.transitionTo('app');
})
.controller( 'AppCtrl', function AppCtrl ( $scope, Auth ) {
console.log('greetings from AppCtrl');
});
foo.js
angular.module( 'ngBoilerplate.foo', ['ui.state'])
.config(function config( $stateProvider ) {
$stateProvider
.state( 'app.foo', {
url: '/foo/:type',
views: {
"main": {
controller:'FooCtrl',
templateUrl: function(stateParams) { /* stuff is going on in here*/ }
}
}
});
})
.controller( 'FooCtrl', function FooCtrl( $scope ) {
console.log('deferred foo');
});
How do i make this work or what other approaches could i take to have something global resolved before every state (without defining a resolve on each state)?
$stateProvider. The scenario you (and I) have is the module that defines the root state (in this scenario stateappin module 'ngBoilerplate) requires another module (in this casengBoilerplate.foo) that defines a state that is a child of the said root state (i.e.app.foo). All of the first module's (ngBoilerplate) dependencies need to be resolved first (which includes running all their.configs to be run) before it runs its own.config`.$stateProvidercould only allow for the defining of child states, even if the parent state hasn't yet been defined, and instead throw a fit after the config stage if there is a child state with no parent state defined.Uncaught Error: No such state 'app' from testbecause the$stateProvider.state('app', {...});in theappmodule's config hasn't been run yet. Sydney's answer doesn't help with the issue at hand.appmodule sinceapp.teststate depends onappstate. Sovar module = angular.module('app', ['ui.state']);andvar test = angular.module('test', ['app']);, your Plunkr now complains about the missing controller which is correct.