Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ui-router. get state name from object/function

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.

like image 686
Eugene Kostrikov Avatar asked May 11 '14 09:05

Eugene Kostrikov


1 Answers

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:

  1. list view and
  2. detail view.

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];
          }],
        }
      }});

}])
like image 142
Radim Köhler Avatar answered Oct 31 '22 23:10

Radim Köhler