我想延迟 AppCtrl 的初始化和任何路由更改,直到“应用程序设置”从服务器到达。
我正在使用 AngularJS-boilerplate,其中包括ui-route和ui-state。到目前为止,我发现的所有解决方案都没有真正推迟 AppCtrl 的启动和路由。
所有的console.log 都会在启动时触发,除了“at resolve”。
Javascript app.js
var app = angular.module( 'ngBoilerplate', [
'templates-app',
'templates-common',
'ngBoilerplate.foo',
'ui.state',
'ui.route'
]);
app.config( function myAppConfig ( $stateProvider, $urlRouterProvider, $translateProvider ) {
console.log('greetings from config');
// Routes
$stateProvider.state('home', {
controller:"AppCtrl",
resolve:{
app: function ($q, $timeout) {
var defer = $q.defer();
console.log('before resolve');
$timeout(function () {
console.log('at resolve');
defer.resolve();
}, 2000);
return defer.promise;
}
}
});
$urlRouterProvider.when('/foo','/foo/tile');
$urlRouterProvider.otherwise( '/foo' );
});
app.run(function run( $rootScope, $state, $stateParams, titleService, dataFactory ) {
console.log('greetings from run');
$state.transitionTo('home');
});
var AppCtrl = app.controller( 'AppCtrl', function AppCtrl ( $scope, $location ) {
console.log('greetings from AppCtrl');
});
Javascript foo.js
angular.module( 'ngBoilerplate.foo', [
'ui.state',
'placeholders',
'ui.bootstrap'
])
.config(function config( $stateProvider ) {
$stateProvider
.state( 'foo', {
url: '/foo/:type',
views: {
"navigation": {
templateUrl: "navigation/navigation.tpl.html"
},
"footer": {
templateUrl: "navigation/footer.tpl.html"
},
"main": {
controller:'FooCtrl',
templateUrl: function(stateParams) {
if(stateParams.type == null) {
stateParams.type = 'tile';
}
return "foo/foo."+(stateParams.type == null || stateParams.type == 'tile' ? 'tile' : 'list')+".tpl.html";
}
}
}
})
.controller( 'FooCtrl', function FooCtrl( $scope ) {
console.log('deferred FooCtrl');
});