简体   繁体   English

使用Angular UI路由器,如何使状态成为所有状态的子状态?

[英]With Angular UI Router, how do make a state a child of all states?

I'd like to make modal dialog boxes into a separate state within Angular UI Router. 我想在Angular UI Router中将模态对话框变成一个单独的状态。 However, I have a case where the modal box can/should appear on top of all states... and I am not sure if this is possible. 但是,我有一个案例,模态框可以/应该出现在所有状态之上......我不确定这是否可行。

So for example, let's say I have /store and /admin as 2 separate top-level states. 例如,假设我将/store/admin作为2个单独的顶级状态。 Now imagine that I have a modal dialog that is unrelated, like a contact form (just as an example) that should go on top of each. 现在想象一下,我有一个不相关的模态对话框,比如一个联系表单(就像一个例子)应该放在每个对话框之上。

If I model this as /contact , the /store or /admin states are obviously forgotten, so the background behind the modal is blank. 如果我将其建模为/contact/store/admin状态显然是被遗忘的,因此模态背后的背景是空白的。

The solution might be to have two child states, like /store/contact and /admin/contact , and this preserves the content behind the modal dialog box... however, this approach does not scale for X number of top-level states. 解决方案可能是有两个子状态,如/store/contact/admin/contact ,这样可以保留模态对话框后面的内容......但是,这种方法不能扩展X个顶级状态。

In a nutshell, how can I say that /contact is a child of all states, regardless of nesting? 简而言之,我怎么能说/contact是所有州的孩子,无论是否嵌套? Is this possible? 这可能吗? Or do I simply have to not use angular UI router in this case? 或者我只是不必在这种情况下使用角度UI路由器?

The UI-Router state machine is expressed with parent-child relations. UI-Router状态机用父子关系表示。 So the answer is - each child can have just/exactly one Parent . 所以答案是 - 每个孩子可以只有/只有一个父母 That's a fact. 这是事实。 And it could be treated as "kind of not working answer" . 它可以被视为“一种不工作的答案”

But, in case that we really would like to have many child states /contact (where contact feature is moved to its own state) , I would not say that repeating of definition is an issue. 但是,如果我们真的希望有很多子状态/contact (where联系人feature is moved to its own state) ,我不会说重复定义是一个问题。

It could be marked as overhead, but state is just a configuration . 它可以标记为开销,但状态只是一种配置 It does not affect the performance so much. 它不会对性能产生太大影响。 The views and controllers are instantiated per state anyhow... regardless of its state def. 无论如何,每个状态都会实例化视图控制器 ......无论其状态如何。

// declare once view
var contactState = {
  controller: ...
  template: ...
  ...
};

// reuse as need for some ...
stateNamesWithContact.forEach( function(stateName)
{
    // regiester state many times as "some.parnet.child.contact"
    $stateProvider
        .state(stateName + ".contact", contact);
    ...

Another approach, which I would perfer, is to NOT use Contact as a substate. 我认为另一种方法是使用Contact作为子状态。 Move it into view (which functionality will support modals) 将其移动到视图中(哪些功能将支持模态)

Multiple Named Views 多个命名视图

Defined once, in root state - as a view 在根状态下定义一次 - 作为视图

In case, that Contact view/controller are static, we can just hook them into supper root state: 如果Contact视图/控制器是静态的,我们可以将它们挂钩到超级根状态:

.state("root", {
    views : {        
        "" : {
           template: "<ui-view />" // here will be injected all children
        }
        "contact@" : {
           ... //setting of the Contact view / controller
        }
    }
}

Now, each "parent" state can profit from the above, without having affected url or name (no need to have "root.someParent" if parent setting is used) 现在,每个“父”状态都可以从上面获益,而不会影响urlname (如果使用parent设置则不需要“root.someParent”)

.state("someState", {
    parent: "root"
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM