【发布时间】:2015-08-12 20:21:36
【问题描述】:
我使用 ui-router 并有两个嵌套视图。 当用户进入子状态时,我想在父视图中隐藏一些 html 内容,并在用户返回父视图时再次显示。 任何人都可以提出如何实现这一目标的建议吗?
使用根范围和状态更改事件很容易做到这一点,但它看起来很脏,不是吗?
app.js
'use strict';
var myApp = angular.module('myApp', ['ui.router']);
myApp.controller('ParentCtrl', function ($scope) {
$scope.hideIt = false;
});
myApp.controller('ChildCtrl', function ( $scope) {
$scope.$parent.hideIt = true;
});
myApp.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('parent', {
url: '/parent',
templateUrl: 'parent.html',
controller: 'ParentCtrl'
})
.state('parent.child', {
url: '/child',
template: '<h2>Child view</h2>',
controller: 'ChildCtrl'
});
});
parent.html
<h2>Parent view</h2>
<div ng-hide="hideIt">
<p>This text should be hidden when the user goes to the nested state.</p>
</div>
<a ui-sref="parent.child">Go to the nested view</a>
<div ui-view></div>
【问题讨论】:
-
能否包含一些父视图和路由代码?
-
@wesww ,我已经加入了
-
好的,我很确定你现在在做什么
标签: angularjs angular-ui-router