13

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

显然,这应该可以根据这个常见问题解答: 如何:从多个模块配置 ui-router

对我来说它不起作用: 错误未捕获错误:ngBoilerplate.foo中没有这样的状态“应用程序”

这是我所拥有的:

应用程序.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');
});

我如何使这项工作或我可以采取哪些其他方法在每个状态之前全局解决某些问题(而不在每个状态上定义一个解决方案)?

4

3 回答 3

7

我最终选择了这种为我工作的方法:

// 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">
于 2013-09-17T07:07:22.250 回答
1

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

angular.module( 'ngBoilerplate.foo', ['ngBoilerplate'])
于 2013-08-15T16:51:33.717 回答
0

我只想发表评论,但我没有代表。我知道这很旧,但我遇到了同样的问题并遇到了这个问题。我感到困惑的一件事是在 app.js 中,您没有导入“ngBoilerplate.foo”,而是导入了 ngBoilerplate.library。我遇到了同样的问题,我的解决方案是将子模块注入顶部模块而不是它们的父模块。

我的结构是模块('ngBoilerplate'),模块('ngBoilerplate.foo')和模块('ngBoilerplate.foo.bar')'。我将 ngBoilerplate.foo.bar 注入 ngBoilerplate.foo 并且 $stateProvider 失败了。我需要将 ngBoilerplate.foo.bar 注入顶级 ngBoilerplate。

我想我会把这个放在这里以防其他人看到这个。我遇到的错误是 Uncaught TypeError: Cannot read property 'navigable' of undefined from ngBoilerplate.foo

于 2013-09-16T21:11:46.487 回答