19

我只想首先说我查看了尽可能多的与此问题相关的堆栈溢出问题,并且我没有看到任何关于我遇到的问题的问题。有些是相似的,但不是真的。这是问题:

我设置了以下 $stateProvider:

$stateProvider.
    state('root', {
        url: '',
        views: {
            'ViewA': {
                templateUrl: 'ViewA.html',
                controller: ViewAController
            },
            'ViewB': {
                templateUrl: 'ViewB.html',
                controller: ViewBController
            },
            'ViewC': {
                templateUrl: 'ViewC.html',
                controller: ViewCContoller
            }
        }
    });

以下是与之配套的 index.Html:

<head>
    <title>Angular PoC</title>       
</head>
<body ng-controller="DashboardController">
    <!-- Account View for logging in -->
    <div ui-view="ViewA" ng-hide="currentlyLoggedIn"></div>

     <!-- Used to toggle the two main views -->
    <div ng-show="currentlyLoggedIn">
        <button ng-click="activateViewB()"> View B </button> 
        <button ng-click="activateViewC()"> View C </button>
    </div>

    <!-- These are the two main views -->
    <div ui-view="ViewB" ng-show="currentlyLoggedIn && currentViewB"></div>
    <div ui-view="ViewC" ng-show="currentlyLoggedIn && currentViewC"></div>
</body>

这是 ViewC.html:

<div>
    This is ViewC
    <div ui-view="ViewCDetails"></div>
</div>

我需要让所有 ViewA、B、C 基本上相互独立,基本上它们都是各自的状态机。它们都需要保持相同的视图,因为所有三个都需要保持它们的状态,即使其他一个发生变化。我遇到的最大问题是我无法初始化“ViewCDetails”,并且我无法添加任何影响其中一个视图的状态,而无需冲洗其他两个视图。有没有办法添加只影响一个视图而不会对其他两个视图产生不利影响的状态?还要注意的是,这应该是一个 SPA,所以它不是 URL 驱动的。

4

1 回答 1

43
$stateProvider.
    state('root', {
        url: '',
        views: {
            'ViewA': {
                templateUrl: 'ViewA.html',
                controller: ViewAController
            },
            'ViewB': {
                templateUrl: 'ViewB.html',
                controller: ViewBController
            },
            'ViewC': {
                templateUrl: 'ViewC.html',
                controller: ViewCContoller
            },
            'ViewCDetails@root': {
                templateUrl: 'ViewCDetails.html',
                controller: ...
            }
        }
    });

https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views

于 2014-01-10T16:58:58.547 回答