AngularJS, ui-router, nested states

eflat

I'm building the outline of an app using ui-router. I've defined the states and it seems to work, but I can't help but feel there might be a better way in terms of best practice.

My states:

  • 'main' is an abstract state with a header, footer, and a middle content area.
  • 'main.home' is what comes up by default. The content file 'home.tpl.html' is a sort of splash page with srefs to other areas of the app, like 'main.wizard.step1'.
  • 'main.wizard' is an abstract state representing a multi step info gathering wizard.
  • 'main.wizard.step1', 'main.wizard.step2' are steps in the wizard

What I'm doubting is my use of the 'views' object having values of "@" and "". Does this look reasonable, or would you do something else?

$urlRouterProvider.otherwise('/');

var app = {
    name: 'main',
    abstract: true,
    url: '',
    views: {
        'header': {
            templateUrl: 'header.tpl.html'
        },
        'footer': {
            templateUrl: 'footer.tpl.html'
        }
    }
};

var home = {
    name: 'main.home',
    url: '/',
    views: {
        "@": {
            templateUrl: 'home/home.tpl.html'
        }
    }
};

var wizard = {
    name: 'main.wizard',
    abstract: true,
    url: '/wizard',
    views: {
        "@": {
            templateUrl: 'wizard/wizard.tpl.html'
        }
    }
};

var step1 = {
    name: 'main.wizard.step1',
    url: '/step1',
    views: {
        "": {
            templateUrl: 'wizard/step1.tpl.html'
        }
    }
};

/** repeat similarly for step2, step3 & step 4 **/

$stateProvider.state(app);
$stateProvider.state(home);
$stateProvider.state(wizard).state(step1).state(step2).state(step3).state(step4);
Gabriel Kohen

What '@' will mean in the definition of the template of the view will be injected into the ui-view of the root state which is usually your index.html. If you want the wizard to go into your middle area of your main page you should add something like this to your index.html:

<ui-view name='content'>Optional placeholder text</ui-view>

And in the definition of the view you should do something like

var wizard = {
name: 'main.wizard',
abstract: true,
url: '/wizard',
views: {
    "@content": {
        templateUrl: 'wizard/wizard.tpl.html'
    }
 }
};

You can actually drop that @ in @content because main is the parent of wizard and you don't have to resolve it in an absolute manner. In any case if you want to have steps in your wizard(of course) don't put any views in your wizard virtual state. Have your steps sub states have their own view and target wizard@. I don't see that you need a separate view wrapper for your wizard (Maybe if you want to do "step x of y" or a progress bar). Hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS UI Router shared nested routes/states

From Dev

Having troubles with nested states in AngularJS and ui-router

From Dev

Angular UI-Router and nested states

From Dev

Angular UI Router - Nested States with multiple layouts

From Dev

URL Routing for nested states in ui-router

From Dev

nested states using Angular ui-router

From Dev

How to get Angularjs UI Router list of states?

From Dev

angularjs - ui.router sibling states

From Dev

angularjs - ui.router sibling states

From Dev

Angularjs ui.router chid states not loading

From Java

Angular-ui-router: ui-sref-active and nested states

From Dev

What is the minimum needed to preserve angularjs ui-router states with sticky states from ui-router-extras?

From Dev

Angular UI-router, nested states don't show

From Dev

Nested states or views for layout with leftbar in ui-router?

From Dev

How to setup a parent/child with a resolve and nested states in ui-router?

From Dev

Ionic - [ui.router] can't navigate nested states

From Dev

Angular UI Router Nested State resolve in child states

From Dev

angular-ui-router nested states "no such state" for parent

From Dev

Angular UI Router: nested states for home to differentiate logged in and logged out

From Dev

How to put nested states inside multiple views with UI-Router

From Dev

Controller executing twice with ui-router nested states (Ionic)

From Dev

Walking by nested states in ui-router without reloading parent state

From Dev

How to setup a parent/child with a resolve and nested states in ui-router?

From Dev

AngularJs: Loop through / return all states in $stateProvider / ui-router

From Dev

[AngularJS][ui-router]: optional params in states in the middle of URL

From Dev

How to write common header & footer in Angularjs using ui router states

From Java

AngularJS ui router passing data between states without URL

From Dev

AngularJS ui-router default state for states without a URL

From Dev

Set Default View for multiple states AngularJS ui.router

Related Related

  1. 1

    AngularJS UI Router shared nested routes/states

  2. 2

    Having troubles with nested states in AngularJS and ui-router

  3. 3

    Angular UI-Router and nested states

  4. 4

    Angular UI Router - Nested States with multiple layouts

  5. 5

    URL Routing for nested states in ui-router

  6. 6

    nested states using Angular ui-router

  7. 7

    How to get Angularjs UI Router list of states?

  8. 8

    angularjs - ui.router sibling states

  9. 9

    angularjs - ui.router sibling states

  10. 10

    Angularjs ui.router chid states not loading

  11. 11

    Angular-ui-router: ui-sref-active and nested states

  12. 12

    What is the minimum needed to preserve angularjs ui-router states with sticky states from ui-router-extras?

  13. 13

    Angular UI-router, nested states don't show

  14. 14

    Nested states or views for layout with leftbar in ui-router?

  15. 15

    How to setup a parent/child with a resolve and nested states in ui-router?

  16. 16

    Ionic - [ui.router] can't navigate nested states

  17. 17

    Angular UI Router Nested State resolve in child states

  18. 18

    angular-ui-router nested states "no such state" for parent

  19. 19

    Angular UI Router: nested states for home to differentiate logged in and logged out

  20. 20

    How to put nested states inside multiple views with UI-Router

  21. 21

    Controller executing twice with ui-router nested states (Ionic)

  22. 22

    Walking by nested states in ui-router without reloading parent state

  23. 23

    How to setup a parent/child with a resolve and nested states in ui-router?

  24. 24

    AngularJs: Loop through / return all states in $stateProvider / ui-router

  25. 25

    [AngularJS][ui-router]: optional params in states in the middle of URL

  26. 26

    How to write common header & footer in Angularjs using ui router states

  27. 27

    AngularJS ui router passing data between states without URL

  28. 28

    AngularJS ui-router default state for states without a URL

  29. 29

    Set Default View for multiple states AngularJS ui.router

HotTag

Archive