何时使用ui-router使用子状态vs多个视图

时间:2014-08-13 06:34:30

标签: angularjs angular-ui-router

上个几小时我一直在阅读UI-Router的文档。但我无法找到解决问题的方法。

我的webapp有两个不同的列,左边是列表,右边是详细视图。选择列表元素应在右侧显示详细信息。

您更喜欢标题中描述的这两种方法中的哪一种?什么时候用?

1 个答案:

答案 0 :(得分:11)

实际上, List x Detail 场景最适合ui-router。这些实际上是两个州,即父母/孩子(即回答问题的孩子状态)

  • a List view (例如左栏)。这可能是一个动态视图,有分页,排序和过滤,但仍然 - 这将始终是一个网关,父母:
  • a Detail view (例如右栏)。要选择详细信息(除非直接通过网址导航),我们只需要一个List view。要选择不同的细节,我们可以从一个事实中获益,即父/ List view状态不重新加载,同时在许多细节中进行迭代......

我们所能做的最好的就是观察由ui-router团队提供的示例:

我们也可以看到它的定义,这是该州定义的一部分:

这个链接,属于我记得的最好的文档代码片段......它解释了所有内容,也有助于了解ui-router状态定义是如何工作的。

下面我试图通过引用List和Detail状态的定义来显示这种力量。

列表状态:

/////////////////////
// Contacts > List //
/////////////////////
// Using a '.' within a state name declares a child within a parent.
// So you have a new state 'list' within the parent 'contacts' state.
.state('contacts.list', {
    // Using an empty url means that this child state will become active
    // when its parent's url is navigated to. Urls of child states are
    // automatically appended to the urls of their parent. So this state's
    // url is '/contacts' (because '/contacts' + '').
    url: '',
    // IMPORTANT: Now we have a state that is not a top level state. Its
    // template will be inserted into the ui-view within this state's
    // parent's template; so the ui-view within contacts.html. This is the
    // most important thing to remember about templates.
    templateUrl: 'app/contacts/contacts.list.html'
})

详情状态:

///////////////////////
// Contacts > Detail //
///////////////////////
// You can have unlimited children within a state. Here is a second child
// state within the 'contacts' parent state.
.state('contacts.detail', {
    // Urls can have parameters. They can be specified like :param or {param}.
    // If {} is used, then you can also specify a regex pattern that the param
    // must match. The regex is written after a colon (:). Note: Don't use capture
    // groups in your regex patterns, because the whole regex is wrapped again
    // behind the scenes. Our pattern below will only match numbers with a length
    // between 1 and 4.
    // Since this state is also a child of 'contacts' its url is appended as well.
    // So its url will end up being '/contacts/{contactId:[0-9]{1,8}}'. When the
    // url becomes something like '/contacts/42' then this state becomes active
    // and the $stateParams object becomes { contactId: 42 }.
    url: '/{contactId:[0-9]{1,4}}',
    // If there is more than a single ui-view in the parent template, or you would
    // like to target a ui-view from even higher up the state tree, you can use the
    // views object to configure multiple views. Each view can get its own template,
    // controller, and resolve data.
    // View names can be relative or absolute. Relative view names do not use an '@'
    // symbol. They always refer to views within this state's parent template.
    // Absolute view names use a '@' symbol to distinguish the view and the state.
    // So 'foo@bar' means the ui-view named 'foo' within the 'bar' state's template.
    views: {
        // So this one is targeting the unnamed view within the parent state's template.
        '': {
            templateUrl: 'app/contacts/contacts.detail.html',
            controller: ['$scope', '$stateParams', 'utils',
            function ( $scope, $stateParams, utils) {
                $scope.contact = utils.findById($scope.contacts, $stateParams.contactId);
            }]
        },
        // This one is targeting the ui-view="hint" within the unnamed root, aka index.html.
        // This shows off how you could populate *any* view within *any* ancestor state.
        'hint@': {
            template: 'This is contacts.detail populating the "hint" ui-view'
        },
        // This one is targeting the ui-view="menu" within the parent state's template.
        'menuTip': {
            // templateProvider is the final method for supplying a template.
            // There is: template, templateUrl, and templateProvider.
            templateProvider: ['$stateParams',
            function ( $stateParams) {
                // This is just to demonstrate that $stateParams injection works for templateProvider.
                // $stateParams are the parameters for the new state we're transitioning to, even
                // though the global '$stateParams' has not been updated yet.
                return '<hr><small class="muted">Contact ID: ' + $stateParams.contactId + '</small>';
            }]
        }
    }
})

摘要:在这些情况下,请使用父/子状态定义,因为父项将只加载一次,并保留其数据,同时我们在其子项之间进行迭代

查看这些链接以获取更多详细信息: