Skip to content

RFC: Leaving specified views intact during state transitions #562

@MrOrz

Description

@MrOrz

I would like to send a pull request proposing a solution to #63 and #317, with two lines of code modification.

MrOrz/ui-router@angular-ui:master...patch-1

Summary of Changes

The template string of a state can be set to =. When entering such state, the named views with its template set to = will remain intact. Neither the scope nor the DOM will be destructed.

When = is introduced, implementing modal dialogs with ui-router can be straight-forward. I humbly provide a few motivating examples in the following sections. For each example, click "Edit in JSBin" button on the upper right corner to view its source.


Usage Example

// A page that lists all the items.
// Clicking the items will show the details in a modal dialog.
//
$stateProvider.state('index', {
  url: '/',
  templateUrl: 'index.html'
});

// The detail pages. 
// Populates "modal" view and keeps the main view.
//
$stateProvider.state('detail', {
  url: '/detail/:id',
  views: {
    '': {
      template: '='
    },
    modal: {
      templateUrl: 'modal.html',
      controller: modalCtrl
    }
  }
});

HTML Setup: Two ui-views, side-by-side.

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

Proof of Concept

http://jsbin.com/AYoKuHe/latest#/
Each "detail page" can be accessed individually via URLs. All the states are correctly managed by ui-router.

Notice that when the modal opens, the underlying index is still visible.

This is achieved by setting the template of main ui-view to = while in the "detail" state.

When the detail page is accessed directly via URL, the underlying page is blank.
http://jsbin.com/AYoKuHe/latest#/detail/4

Adding some simple trick and CSS magic, the detail pages can be shown outside of a modal when their URLs are directly accessed.
http://jsbin.com/itOjaj/latest#/detail/2

Notice the detail pages can still be viewed in modal after navigating to the index page. The behaviour is exactly the same as Facebook albums and Instagram colorboxes.

Integration with ui-modal

See it in action: http://jsbin.com/aFemAfe/latest
Individual detail pages are shown outside the modal: http://jsbin.com/aFemAfe/latest

The trick here is to move ui-view="modal" around. Kind of stupid, but it is somewhat better than fighting bugs caused by preventing state changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions