简体   繁体   English

Ui-Router深层嵌套状态与父母

[英]Ui-Router Deep Nested States with Parents

I have been searching around, and have found nothing that works as I need yet, or anything that works in general. 我一直在四处搜寻,发现没有什么能像我需要的那样起作用,或者任何有效的东西。

I have states for an application I am building, as follows: 我有一个我正在构建的应用程序的状态,如下所示:

app - Main Parent State app - 主要父母状态

facebook - Parent state of all routes for Facebook facebook - Facebook所有路线的父状态

adwords - Parent state of all routes for Adwords adwords - Adwords所有路由的父状态

bing - Parent state of all routes for Bing bing - Bing所有路由的父状态

... - There are a lot more... A lot... ... - 还有很多......很多......

So, for instance, if I wanted a Facebook overview, it would be app.facebook.overview . 所以,例如,如果我想要一个Facebook概述,它将是app.facebook.overview However, it hasn't worked in any ways I have tried ( dot annotation, parent in the declaration, etc.) 但是,它没有以我试过的任何方式工作( dot注释,声明中的parent等)

$stateProvider
    .state('app', {
        url: '',
        abstract: true,
        templateUrl: 'assets/views/default.html'
    })
    .state('app.facebook', {
        url: '/facebook',
        abstract: true
    })
    .state('app.facebook.overview', {
        url: '/overview',
        templateUrl: '/assets/views/facebook/overview/overview.html',
        controller: 'FacebookOverviewCtrl'
    });

I have tried every combination of abstract and parent , along with the dot annotation. 我已经尝试了abstractparent每个组合,以及dot注释。 I haven't been able to get it to work. 我无法让它发挥作用。 It fails giving me the following error: 它没有给我以下错误:

Could not resolve 'app.facebook.overview' from state 'app'

What am I doing wrong here? 我在这做错了什么? I don't understand, I have followed documentation for it, along with looked at numerous questions here, on Stackoverflow. 我不明白,我已经关注它的文档,并在Stackoverflow上查看了很多问题。 Any ideas? 有任何想法吗?

EDIT: 编辑:

I have figured out the problem. 我已经找到了问题所在。 Elementary mistake. 基本错误。 I didn't include the module in the main angular.module declaration. 我没有在主angular.module声明中包含该模块。 Well... 4 am coding for you. 嗯...凌晨4点为你编码。

I'm basing my answer on this one here Basically, you just need to set the url of /facebook to url: ':facebook' and that should do the trick. 我立足我的回答就这一个在这里基本上,你只需要设置的网址/facebookurl: ':facebook'和应该做的伎俩。 http://plnkr.co/edit/LQLYTu0YHlvTdB26vQcP http://plnkr.co/edit/LQLYTu0YHlvTdB26vQcP

I would like to state, for the fact, that I am stupid. 我想说的是,我是愚蠢的。

Here is a plunk with it working: https://plnkr.co/edit/aAeoucDidw3NX7QJqise?p=preview 这是一个有效的插件: https ://plnkr.co/edit/aAeoucDidw3NX7QJqise?p = preview

It would have helped in my code if I included the module. 如果我包含该模块,它将有助于我的代码。

var App = angular.module('app', [
    '... other modules ...',
    'ui.router',
    '... other modules ...',
]);

Fix: 固定:

var App = angular.module('app', [
    '... other modules ...',
    'ui.router',
    'app.facebook',
    '... other modules ...',
]);

Elementary mistake, but worth keeping it here since it took me quite a while to figure out how to do deep nested states with ui-router . 基本错误,但值得保留在这里,因为我花了很长时间才弄清楚如何使用ui-router进行深度嵌套状态。 Some things I learned: 我学到的一些东西:

  • You do NOT need to have a URL on abstract states, but if you do, it prepends down 您不需要在抽象状态上使用URL,但如果这样做,则会预先设置

    • For instance: app has no url, app.facebook has a url of '/facebook' , and app.facebook.overview has a url of '/overview' . 例如: app没有网址, app.facebook的网址为'/facebook'app.facebook.overview的网址为'/overview' Meaning the url that will be used will end up being /facebook/overview 这意味着将要使用的URL最终将成为/facebook/overview
  • You can have states nested as deep as you want. 您可以根据需要将状态嵌套为深度。

  • If you go further down, you need to include template: '<ui-view></ui-view>' on your state declaration, so it will continue to input the next state as needed. 如果进一步向下,则需要在状态声明中包含template: '<ui-view></ui-view>' ,因此它将根据需要继续输入下一个状态。

    • For instance: app has a template page, that has <ui-view></ui-view> in it, but app.facebook must also have template: '<ui-view></ui-view>' declared so it will insert the page you want below it ( app.facebook.overview ). 例如: app有一个模板页面,其中包含<ui-view></ui-view> ,但是app.facebook也必须有template: '<ui-view></ui-view>'声明如此将在其下方插入您想要的页面( app.facebook.overview )。

I hope this helps more people than just me. 我希望这能帮助更多的人而不仅仅是我。 It took a while to figure it out, but the plunk has the code working in it. 花了一段时间才弄明白,但是plunk的代码工作正常。 Now, that's dealt with. 现在,这是处理。 I feel like I can finally move on... 我觉得我终于可以继续......

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

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