I wonder, is there any way to reference states in view with object or function?
Just to decouple views from states definition. E.g. if I change state name I don't have to change it everywhere in my views.
One solution, described below, could be found here, as a working plunker
In this example we will define state for some entity (e.g. employee) like:
Let's use some variable entityName
to play the role of the decoupled naming:
var entityName = "employee";
$stateProvider
.state(entityName, {
url: '/' + entityName,
views: {
...
}})
.state(entityName + '.detail', {
url: '/{{Id}}',
views: {
...
}});
Navigation from the list to the detail view (As we can see, there is no explicit "employee" name used):
<a ui-sref="{{detailLink(item)}}" >{{item.Name}}</a>
Next we have to define the detailLink(item)
. We will do it directly in the controller
here, but it could be some ListModel
instance instead, encapsulating more operations (paging, sorting), including the detailLink
.
controller:['$scope','$state',
function ( $scope , $state){
$scope.detailLink = function (item){
// here we get the employee in run-time
var currentState = $state.current.name;
var sref = currentState + '.detail({Id:' + item.Id + '})';
return sref;
};
}],
And that's it. It could be even more complex... The complete example code (enclosed below as states defintion) could be found and run here
.config(['$stateProvider',
function( $stateProvider) {
var entityName = "employee";
$stateProvider
.state(entityName, {
url: '/' + entityName,
views: {
body: {
template: '<div>' +
' <h2>List View</h2> ' +
' <ul ng-repeat="item in items"> ' +
' <li><a ui-sref="{{detailLink(item)}}" >{{item.Name}}</a>' +
' </li></ul>' +
' <h2>Detail View</h2> ' +
' <div ui-view="detail"></div>' +
'</div>',
controller:['$scope','$state',
function ( $scope , $state){
$scope.items = [{Name : "Abc", Id : 0}, {Name : "Def", Id : 1}];
$scope.detailLink = function (item){
var currentState = $state.current.name;
return currentState + '.detail({Id:' + item.Id + '})';
};
}],
}
}})
.state(entityName + '.detail', {
url: '/{{Id}}',
views: {
detail: {
template: '<div>' +
' <label>{{item.Name}} ' +
' <input ng-model="item.Name"}}" type="text" />' +
' <div>current state name: <i>{{state.name}}<i></div> ' +
'</div>',
controller:['$scope','$stateParams','$state',
function ( $scope , $stateParams , $state){
$scope.state = $state.current
$scope.item = $scope.items[$stateParams.Id];
}],
}
}});
}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With