【问题标题】:angular ui-router - nested controller - parent controller is initiated twice if child controller is in the same stateangular ui-router - 嵌套控制器 - 如果子控制器处于相同状态,则父控制器启动两次
【发布时间】:2016-12-06 21:16:30
【问题描述】:
.state('home', {
    url: '/home',
    views: {
        'main': {
            templateUrl: './main.html',
            controller: 'mainController'
        },
        'parent@home': {
            templateUrl: './parent.html',
            controller: 'parentController'
        },
        'child@home':{
            templateUrl: './child.html',
            controller: 'childController'
        },
        'otherPage@home': {
            templateUrl: './other-page.html',
            controller: 'otherController'
        }
    }
})

在 parent.html 我有子视图。基本上,父控制器(parentController)在此设置中启动了两次。如果我删除 child@home 状态,则 parentController 仅启动一次。我做错了什么,有没有更好的方法来定义这些状态?

index.html

<div class="maincontent" ui-view="main"></div>

main.html

<div class="maincontent container-fluid" ui-view="parent"></div>
<div class="maincontent container-fluid" ui-view="otherPage"></div>

parent.html

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

【问题讨论】:

  • 你没有定义任何状态!
  • 也许您可以显示您的模板标记?这将使某人对您的问题有更好的想法/理解
  • ragingprodigy,我已经添加了模板标记,如果您还需要什么,请告诉我

标签: javascript html angularjs angular-ui-router angular-routing


【解决方案1】:

像这样重新定义状态:

.state('home', {
    url: '/home',
    views: {
        '': {
            templateUrl: './main.html',
            controller: 'mainController'
        },
        'parent@home': {
            templateUrl: './parent.html',
            controller: 'parentController'
        },
        'child@home':{
            templateUrl: './child.html',
            controller: 'childController'
        },
        'otherPage@home': {
            templateUrl: './other-page.html',
            controller: 'otherController'
        }
    }
})

根据我的理解,带有空键的视图将作为主状态的默认值加载。

通过这种方式,您的main.html 模板将包含指向其他ui-views 的指针,就像您对parentotherPage 所做的那样。

我无法对此进行测试,因为我不在我的开发 PC 上,但我认为它应该可以工作,因为它是我的几个 AngularJS 项目中遵循的格式。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多