I wanted to share with you my approach. There is a working plunker.
Let's have three areas layout - top, left, main. And these could be states:
$stateProvider
.state('index', {
url: '/',
views: {
'@' : {
templateUrl: 'layout.html',
controller: 'IndexCtrl'
},
'top@index' : { templateUrl: 'top.html',},
'left@index' : { templateUrl: 'left.html',},
'main@index' : { templateUrl: 'main.html',},
},
})
.state('index.list', {
url: '^/:category',
templateUrl: 'list.html',
controller: 'ListCtrl'
})
.state('index.list.detail', {
url: '/:id',
views: {
'detail@index' : {
templateUrl: 'detail.html',
controller: 'DetailCtrl'
},
},
})
The index will create a scope which will be inherited into each child and grand child state. So we can use $scope.Model
to keep the data consolidated all the way down.
These would be controllers:
.controller('IndexCtrl', ['$scope', function($scope){
$scope.Model = {
categories : ["Music", "Video"],
}
}])
.controller('ListCtrl', ['$scope', '$stateParams', function($scope, $stateParams){
// add items to the model
$scope.Model.items = $stateParams.category === "Music"
? ["Depeche Mode", "Nick Cave"]
: ["Star Wars", "Psycho"]
$scope.$on("$destroy", function() {delete $scope.Model.items; })
}])
.controller('DetailCtrl', ['$scope', '$stateParams', function($scope, $stateParams){
// add item to model
$scope.Model = {
item : $scope.Model.items[$stateParams.id],
}
$scope.$on("$destroy", function() {delete $scope.Model.item; })
}])
What is happening here? Partially we use $stateParams to change states (category
is passed to List ... id
into Detail)
Also all states (all the way down) do have acccess to the same instance (reference) of the $scope.Model
Check it all here
Also, what happened here, is the real use of $scope inheritance, which in UI-Router is driven by view nesting. Please, check this detailed description