How do I load UI-router ui-view templates that are

2020-07-27 02:30发布

问题:

I am attempting to design the routing and nested views in my app several levels deep in order to replace parts of the app based on a route. I'm able to load the login page, and it seems the layout view loads as well, however, I can't seem to get the nested <ui-view /> tags to work. Can someone please tell me how I'm doing this wrong, or if there is a more idiomatic way in Angular to accomplish the same functionality.

app.js

(function() {
  'use strict';



 angular
    .module('webApp', [
      'ui.router',
    ])
    .config(config)
    .run(run);

  config.$inject = ['$stateProvider', '$urlRouterProvider'];

  function config($stateProvider, $urlRouterProvider, ngClipProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
      .state('app', {
        abstract: true,
        url: '/',
        template: '<ui-view/>'
      })
      .state('app.login', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl as login',
        url: ''
      })
      .state('app.main', {
        url: 'room',
        templateUrl: 'views/layout.html'
      })
      .state('app.main.foo', {
        url: '',
        views: {
          'header@app.main': {
            templateUrl: 'partials/header.html',
            controller: 'HeaderCtrl as header'
          },
          'sidebar@app.main': {
            templateUrl: 'partials/sidebar.html',
            controller: 'SidebarCtrl as sidebar'
          },
          'main@app.main': {
            templateUrl: 'partials/main.html',
            controller: 'MainCtrl as main'
          },
          'subHeader': {
            templateUrl: '<div><div ui-view="bottomHeader"></div></div>',
            controller: 'SubHeaderCtrl',
            controllerAs: 'subHeader'
          },
          'subSidebar': {
            templateUrl: '<div><div ui-view="bottomSidebar"></div></div>',
            controller: 'SubSidebarCtrl',
            controllerAs: 'subSidebar'
          },
          'bottomHeader': {templateUrl: '<div>FOO</div>'},
          'bottomSidebar': {templateUrl: '<div>BAR</div>'}
        },
        resolve: {
          isAuthenticated: ['Auth', function(Auth) {
            return Auth.isAuthenticated();
          }]
        }
      })
      .state('app.main.foo.bar', {
        url: '/:id',
        views: {
          'main@': {
            templateUrl: 'partials/main.html'
          },
          'mainOne@': {
            templateUrl: 'partials/main-one.html',
            controller: 'MainOneCtrl as mainOne'
          },
          'mainTwo@': {
            templateUrl: 'partials/main-two.html',
            controller: 'MainTwoCtrl as mainTwo'
          },
          'mainThreee@': {
            templateUrl: 'partials/main-three.html',
            controller: 'MainThreeCtrl as mainThree'
          }
        }
      });
  }

  run.$inject = ['Auth'];

  function run(Auth) {
    Auth.stateChangeError();
    Auth.loginSuccess();
    Auth.loginFailure();
    Auth.checkSession();
  }

}());

index.html

<!DOCTYPE html>

<html lang="en" ng-app="webApp">
  <head>
    <title>FooBar</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="css/main.css" />
    <script src="/js/all.js"></script>
  </head>

  <body ui-view>
  </body>

</html>

layout.html

<div ui-view="header"></div>

<div class="app">
 <div class="getting-started">
 </div>

 <div ui-view="sidebar"></div>

 <div ui-view="main"></div>
</div>

header.html

<header class="header">
  <div ui-view="subHeader"></div>

  <div class="trigger-button">
    <button class="trigger">
      <i class="icon-account"></i>
    </button>
  </div>
</header>

main.html

<div>
  <div ui-view="mainOne"></div>
  <div ui-view="mainTwo"></div>
  <div ui-view="mainThree"></div>
</div>

sidebar.html

<div>
  <div ui-view="subSidebar"></div>
</div>

回答1:

There is a working plunker, showing your scenario

I've made few changes in your state definition:

$stateProvider
  .state('app', {
    abstract: true,
    url: '/',
    template: '<ui-view/>'
  })
  .state('app.login', {
    templateUrl: 'views/login.html',
    controller: 'LoginCtrl',
    controllerAs: 'login',
    url: ''
  })

We are using controller and controllerAs syntax

  .state('app.main', {
    url: 'room',
    templateUrl: 'views/layout.html'
  })

No absulte naming, we target our parent, relative is enough, more readable

  .state('app.main.foo', {
    url: '',
    views: {
      'header': {
        templateUrl: 'partials/header.html',
        controller: 'HeaderCtrl',
        controllerAs: 'header',
      },
      'sidebar': {
        templateUrl: 'partials/sidebar.html',
        controller: 'SidebarCtrl',
        controllerAs: 'sidebar',
      },
      'main': {
        templateUrl: 'partials/main.html',
        controller: 'MainCtrl',
        controllerAs: 'main',
      }
    },
    resolve: {
      isAuthenticated: ['Auth', function(Auth) {
        return Auth.isAuthenticated();
      }]
    }
  })

main state is in our parent, do not redefine it

  .state('app.main.foo.bar', {
    url: '/:id',
    views: {
      'mainOne': {
        templateUrl: 'partials/main-one.html',
        controller: 'MainOneCtrl',
        controllerAs: 'mainOne',
      },
      'mainTwo': {
        templateUrl: 'partials/main-two.html',
        controller: 'MainTwoCtrl',
        controllerAs: 'mainTwo',
      },
      'mainThreee': {
        templateUrl: 'partials/main-three.html',
        controller: 'MainThreeCtrl',
        controllerAs: 'mainThree',
      }
    }
  });

Check it here