Angular.js从多个模块配置ui路由器子状态



我想实现一个设置,在这个设置中,我可以在主模块中定义一个"根状态",然后在其他模块中添加子状态。这是因为我需要先解析根状态,然后才能转到子状态。

显然,根据这个常见问题解答,这应该是可能的:如何:从多个模块配置ui路由器

对我来说,它不起作用:错误未捕获错误:ngBoilerplate.foo 中没有这样的状态"app"

这是我的:

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');
});

我如何做到这一点,或者我可以采取什么其他方法在每个状态之前全局解决问题(而不定义每个状态的解决方案)?

我最终选择了这种为我完成任务的方法:

// add all your dependencies here and configure a root state e.g. "app"
angular.module( 'ngBoilerplate', ['ui.router','templates-app',
'templates-common','etc','etc']);
// configure your child states in here, such as app.foo, app.bar etc.
angular.module( 'ngBoilerplate.foo', ['ngBoilerplate']);
angular.module( 'ngBoilerplate.bar', ['ngBoilerplate']);
// tie everything together so you have a static module name
// that can be used with ng-app. this module doesn't do anything more than that.
angular.module( 'app', ['ngBoilerplate.foo','ngBoilerplate.bar']);

然后在你的应用程序index.html

<html ng-app="app">

在文档中,feature1模块依赖于application模块。尝试

angular.module( 'ngBoilerplate.foo', ['ngBoilerplate'])

我本来想评论一下,但我没有rep。我知道这是旧的,但我遇到了同样的问题,也遇到了这个问题。有一件事我很困惑,那就是在app.js中,你没有导入"ngBoilerplate.foo",而是导入ngBoilelplate.library。我也遇到了同样的问题,我的解决方案是将子模块而不是父模块注入顶部模块。

我的结构是module('ngBoilerplate')、module('nBoilerpate.foo')和module。我需要将ngBoilerplate.foo.bar注入顶级ngBoilelplate。

我想我会把这个放在这里,以防其他人看到。我遇到的错误是Uncaught TypeError:无法从ngBoilerplate.foo 读取未定义的属性"navigative"

最新更新