Angular 的 ui-router 提供了一种干净的方法来切换嵌套视图。
首先将 $state 注入您假设的“列表页面”控制器。然后将其暴露给本地 $scope:
.controller('ListPageCtrl', function($scope, $state) {
$scope.$state = $state;
})
我们注入的 $state 变量有一个很好的“包含”方法。 $state.includes() 将字符串作为参数,并根据当前状态名称检查该字符串。如果状态名称与字符串匹配,则返回 true,否则返回 false。这使它非常适合与 ng-show 或 ng-hide 一起使用。
使用 ui-router 的 $state.includes() 代替与 egervari 相同的模板:
<div id="rightView" ng-show="$state.includes('list.edit')">
<div ui-view="edit" autoscroll="false"></div>
</div>
除了切换到包含方法之外,我还为 ui-view 属性添加了一个唯一名称。一旦你有两个以上的视图,你就会想要命名你的视图。这将使它们更容易在您的模板和逻辑中跟踪。
为您的视图添加名称。将 1 变为 2:
// 1
.state('list.edit', {
url: 'list/edit/:id',
templateUrl: 'template/edit.html',
controller: 'ListPageEditCtrl'
})
// 2
.state('list.edit', {
url: 'list/edit/:id',
views: {
'edit': { // this is the unique name you can reference later
templateUrl: 'template/edit.html',
controller: 'ListPageEditCtrl'
}
}
});