AngularJS UI-Router : Abstract state + child states not working

核能气质少年 提交于 2019-12-23 17:43:29

问题


I'm trying to use an abstract state but I cannot load the child.

My app is setup as following:

angular
  .module('milordApp', [
    'ui.router',
    'ngAnimate',
    'ngCookies',
    'ngMessages',
    'ngResource',
    'ngRoute',
    'ngSanitize',
   'ngTouch'
])
.run(function($rootScope) {
    $rootScope.$on('$routeChangeStart', function(event, toState,  toParams, fromState, fromParams) {
      console.log(toState, toParams, fromState, fromParams);
    });
})
.config(function($stateProvider, $urlRouterProvider){
    $urlRouterProvider.otherwise("/login");
    $stateProvider
      .state('login', {
        url: '/login',
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl',
        data: {
          requireLogin: false
        }
     })
     .state('app', {
       abstract: true,
       url: '/app',
       data: {
        requireLogin: true
       }
     })
     .state('app.dashboard', {
       url: '/dashboard',
       templateUrl: 'views/dashboard.html',
       controller: 'DashboardCtrl',
     });
});

The login route is working fine. However, the app or app.dashboard route does not register as I can't even console.log from the controller.

Just a note that the index.html is creating the navigation correctly

<ul class="nav navbar-nav">
   <li><a ui-sref="login" href="#/login">Login</a></li>
   <li><a ui-sref="app.dashboard" href="#/app/dashboard">Dashboard</a></li>
</ul>

回答1:


In case that snippet of the abstract state is real (as is), the issue should be fixed like this:

.state('app', {
   abstract: true,
   url: '/app',
   data: {
    requireLogin: true
   }
   // add template for child
   template: "<div ui-view></div>",
 })

Simply, every state needs to be injected somewhere. If we do not use named views (or even absolute naming) it is expected that:

every child is injected into its parent target ui-view



来源:https://stackoverflow.com/questions/32741205/angularjs-ui-router-abstract-state-child-states-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!