I'm trying to manage my admin page by bellow states with multi views: admin, admin.header, admin.leftPanel, admin.main, admin.tail. In the header, leftPanel, main and tail, I use $state.go to their sub states respectively to render their contents. I write bellow simple code to demo this problem.
Demo states model:
state1:
state2view
controller: $state.go(state1.state2) <---superseded
state3view
controller: $state.go(state1.state3)
Code (plunker):
<!DOCTYPE html>
<html ng-app="demo">
<head>
<meta charset="utf-8" />
<title>Demo</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.6/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.js"></script>
<script>
let app = angular.module('demo', ['ui.router']);
app.config(['$urlRouterProvider', '$stateProvider', function ($up, $sp) {
$sp.state('state1', state1);
$sp.state('state1.state2', new SubState('state2view'));
$sp.state('state1.state3', new SubState('state3view'));
$up.otherwise('/');
}]);
let state1 = {
url: '/',
views: {
"state1view1": {
controller: ['$transition$', '$state', function ($tr, $st) {
this.stateName = $st.current.name;
$st.go('state1.state2', {message: 'message from ' + $st.current.name + ' to ' + $st.current.name + '.state2'});
}],
controllerAs: '$ctrl',
template: `<div>
{{$ctrl.stateName}} begin<br>
<ui-view name="state2view"></ui-view>
{{$ctrl.stateName}} end
</div>`
},
"state1view2": {
controller: ['$transition$', '$state', function ($tr, $st) {
this.stateName = $st.current.name;
$st.go('state1.state3', {message: 'message from ' + $st.current.name + ' to ' + $st.current.name + '.state3'});
}],
controllerAs: '$ctrl',
template: `<div>
{{$ctrl.stateName}} begin<br>
<ui-view name="state3view"></ui-view>
{{$ctrl.stateName}} end
</div>`
}
}
};
function SubState(view1Name) {
this.params = {message: ''};
this.views = {};
this.views[view1Name] = {
controller: ['$transition$', '$state', function ($tr, $st) {
this.parentMessage = $tr.params().message;
this.stateName = $st.current.name;
}],
controllerAs: '$ctrl',
template: `<div>
{{$ctrl.stateName}} begin<br>
{{$ctrl.parentMessage}}<br>
{{$ctrl.stateName}} end
</div>`
};
}
app.run(function($transitions) {
$transitions.onStart({}, function($tr) {
console.log("trans begin: " + $tr.from().name + " -> " + $tr.to().name);
}
);
$transitions.onSuccess({}, function($tr) {
console.log("trans done: " + $tr.from().name + " -> " + $tr.to().name);
}
);
});
</script>
<style>
div{border-style: solid;}
</style>
</head>
<body>
<ui-view name="state1view1"></ui-view>
<br>
<ui-view name="state1view2"></ui-view>
</body>
</html>
Expected result:
state1 begin
state1.state2 begin
message from state1 to state1.state2
state1.state2 end
state1.state3 begin
message from state1 to state1.state3
state1.state3 end
state1 end
Actual result:
state1 begin
state1 end
state1 begin
state1.state3 begin
message from state1 to state1.state3
state1.state3 end
state1 end