ui-router. get state name from object/function

徘徊边缘 提交于 2019-12-23 11:53:21

问题


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.


回答1:


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

}])


来源:https://stackoverflow.com/questions/23590799/ui-router-get-state-name-from-object-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!