Skip to content
This repository has been archived by the owner on Feb 9, 2018. It is now read-only.

multiple ui-views in default component? #5

Closed
longbowww opened this issue Jul 31, 2016 · 3 comments
Closed

multiple ui-views in default component? #5

longbowww opened this issue Jul 31, 2016 · 3 comments

Comments

@longbowww
Copy link

is it possible to replicate multiple views in the default app state like we could do in angular 1.x?

e.g.

            .state('root', {
                url: '',
                abstract: true,
                views: {
                    'nav': {
                        templateUrl: 'app/core/views/navbar/navbar.html',
                        controller: 'navbarCtrl',
                        controllerAs: 'nav'
                    },
                    'footer': {
                        templateUrl: 'app/core/views/footer/footer.html',
                        controllerAs: 'footer'
                    }
                }
            })

for ng2 i now kinda hacked it and defined a app.home state which maps to /

let MAIN_STATES:Ng2StateDeclaration[] = [
  {
    name: 'app',
    url: '',
    component:AppComponent
  }
];
export let HOME_STATES: Ng2StateDeclaration = [
  {
    name: 'app.home',
    url: '/',
    views: {
      header:{component: NavComponent},
      $default: {component: MainComponent},
      footer: {component: FooterComponent}
    }
  }
];

all tries with abstract and directly defining the app main state failed

@demisx
Copy link

demisx commented Aug 5, 2016

Something like this should work:

// in app.states.ts
const mainStates: Ng2StateDeclaration[] = [
  {
    name: 'app',
    abstract: true,
    views: {
      'header@app': { component: NavComponent },
      $default: { component: AppComponent },
      'footer@app': { component: FooterComponent }
    },
  }
];

export const INITIAL_STATES: Ng2StateDeclaration[] =
  mainStates
    .concat(homeStates);

// in home.states.ts
export const homeStates: Ng2StateDeclaration = [
  {
    name: 'app.home',
    url: '/',
    views: {
      $default: { component: HomeComponent },
    }
  }
];

@christopherthielen
Copy link
Member

christopherthielen commented Aug 5, 2016

Although the quickstart app bootstraps a single UIView:

  <body>
    <ui-view>Loading...</ui-view>
  </body>

you can instead bootstrap a layout component which contains your named views:

  <body>
    <app-layout></app-layout>
  </body>
@Component({
  selector: 'app-layout',
  directives: [UIROUTER_DIRECTIVES],
  template: `
  <ui-view name="nav"></ui-view>
  <ui-view></ui-view>
  <ui-view name="footer"></ui-view>
`
}) 
export class AppLayout { }
var appState =   {
    name: 'app',
    abstract: true,
    views: {
      'header': { component: NavComponent },
      $default: { component: AppComponent },
      'footer': { component: FooterComponent }
    },
  }
}

@christopherthielen
Copy link
Member

Closing because I think this questino is answered.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants