How do I prevent reload on named view, when state changes? AngularJS UI-Router

后端 未结 1 431
误落风尘
误落风尘 2020-12-01 03:59

I\'m using the excellent ui-router module in my application. As part of this, I\'m using named views to manage the \'dynamic sub-navigation\' I have in the app.

相关标签:
1条回答
  • 2020-12-01 04:23

    The way I am using ui-router in this scenarios is: move the views to the least common denominator.

    Other words: In case that ui-view="nav" is shared among all the details and is the same for all of them (because it should be loaded only once) - it should be part of the list state (parent of the detail state)

    the parent state defintion would be adjusted like this:

    .state('person.list', {
        url: '/list',
        views: {
            "main@": {
                templateUrl: "person.list.html",
                controller: 'PersonListController'
            }
            // here we target the person.list.html
            // and its ui-view="nav"
            'nav@person.list': {
                templateUrl: "person.nav.html",
                controller: 'PersonNavController'
            }
        }
    

    So where is the trick? In the power of the angular ui-router. We can, during each state defintion, target the current view. Now, the nav view is part of the list state definition - i.e. it will not be reloaded during the detail switching (also check here for more explanation)

    We just have to use the defined naming conventions, see:

    • View Names - Relative vs. Absolute Names

    Few cited lines from the mentioned documentation:

    views: {
        ////////////////////////////////////
        // Relative Targeting             //
        // Targets parent state ui-view's //
        ////////////////////////////////////
    
        // Relatively targets the 'detail' view in this state's parent state, 'contacts'.
        // <div ui-view='detail'/> within contacts.html
        "detail" : { },            
    
        // Relatively targets the unnamed view in this state's parent state, 'contacts'.
        // <div ui-view/> within contacts.html
        "" : { }, 
    
        ///////////////////////////////////////////////////////
        // Absolute Targeting using '@'                      //
        // Targets any view within this state or an ancestor //
        ///////////////////////////////////////////////////////
    
        // Absolutely targets the 'info' view in this state, 'contacts.detail'.
        // <div ui-view='info'/> within contacts.detail.html
        "info@contacts.detail" : { }
    
        // Absolutely targets the 'detail' view in the 'contacts' state.
        // <div ui-view='detail'/> within contacts.html
        "detail@contacts" : { }
    
    0 讨论(0)
提交回复
热议问题