使用带ui-router的ui-sref指令将变量传递给抽象父状态

Bas*_*erg 5 angularjs angular-ui-router

我正在设置一个简单的angularjs应用程序来管理组,其中我使用ui-router进行嵌套路由.

var groupsApp = angular.module('groupsApp', [
    'ui.router',
]);
Run Code Online (Sandbox Code Playgroud)

每个小组都有参与者和一个计划.该页面/#/groups显示一个列表,其中包含使用该ui-sref指令指向每个组的链接:

<ul>
    <li ng-repeat="group in groups">
        <a ui-sref="group.participants({group_id: {{group.id}}})">{{group.name}}</a>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

状态导航配置如下:

groupsApp.config(
    function($stateProvider) {

        $stateProvider
            .state('groups', {
                url: '/groups',
                templateUrl: 'partials/groups.html'
            })
            .state('group', {
                abstract: true,
                url: '/:group_id',
                templateUrl: 'partials/group.html',
            })
            .state('group.participants', {
                url: '/participants',
                templateUrl: 'partials/participants.html',
            })
            .state('group.programm', {
                url: '/programm',
                templateUrl: 'partials/program.html'
            })
    });
Run Code Online (Sandbox Code Playgroud)

我使用以下控制器:

var myAppControllers = angular.module('myAppControllers', []);

myAppControllers.controller('GroupController', ['$scope', 
  function ($scope) {

    $scope.groups = [
      {'id': 'group1',
       'name': 'My first group',
      {'id': 'group2',
       'name': 'My secord group',
    ];

  }]);
Run Code Online (Sandbox Code Playgroud)

group状态是抽象的,所述group_id确定的URL.对于id为group1和group2的组,可能的url是:

/#/group1/participants
/#/group1/program

/#/group2/participants
/#/group2/program
Run Code Online (Sandbox Code Playgroud)

使用该ui-sref指令,我想导航到组的参与者页面:

<a ui-sref="group.participants({group_id: {{group.id}}})">{{group.name}}</a>
Run Code Online (Sandbox Code Playgroud)

因此,我导航到group.participants状态并传递group_id参数以指示哪个组.但是,这是父状态的参数group.单击该链接会生成以下URL:

/#//participants
Run Code Online (Sandbox Code Playgroud)

显然父类group没有得到这个参数.有谁知道如何实现这一目标?

ps我现在意识到问题可能是角度表达式的嵌套:

{group_id: {{group.id}}}
Run Code Online (Sandbox Code Playgroud)

这是允许的吗?

Rad*_*ler 7

我创造了一个正在运作并且应该遵循您的需求的plunker:在这里

其中一个变化:

<ul>
   <li ng-repeat="group in groups">
      <!--
      <a ui-sref="group.participants({group_id: {{group.id}}})">{{group.name}}</a>
      -->
      <a ui-sref="group.participants({group_id: group.id})">{{group.name}}</a> 
   </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

还有state定义的片段(事实上​​与问题中的相同,但正在运行)

团体定义:

$stateProvider
    .state('groups', {
      url: '/groups',
      template: '<ul> ' +
      '    <li ng-repeat="group in groups"> ' + 
      '     <a ui-sref="group.participants({group_id: group.id})">' +
      '        {{group.name}}</a> ' +
      '   </li> ' +
      ' </ul>',
      controller:['$scope','$state',
        function ( $scope , $state){ 

          $scope.groups = [{name : "My first group" , id : "group1"}
                         , {name : "My second group", id : "group2"}];

      }],
    })
Run Code Online (Sandbox Code Playgroud)

这是group abstract国家.它确实可以访问$stateParams并将它们存储在范围内.所有儿童州也可以访问它.

.state('group', {
    abstract: true,
     url: '/:group_id',
     template: '<div>' +
     '   <h4> group: {{group_id}} <a ui-sref="groups">back</a></h4>' +
     '   <div ui-view></div>' +
     ' </div>',
     controller:['$scope','$state','$stateParams',
       function ( $scope , $state , $stateParams){ 
         $scope.group_id = $stateParams.group_id;
         $scope.params = $stateParams;
         $scope.state = $state.current;
         $scope.showJson = function(x) { return JSON.stringify(x, undefined, 2); };
     }],
 })
Run Code Online (Sandbox Code Playgroud)

孩子说的腹足group,可以消耗已经准备好的东西

 .state('group.participants', {
     url: '/participants',
     template: '<div>' +
     ' <h5>params</h5> ' +
     '   <pre>x{{showJson(params)}}</pre>' +
     ' <h5>state</h5> ' +
     '   <pre>{{showJson(state)}}</pre>' +
     ' </div>',
 })
 .state('group.programm', {
     url: '/programm',
     template: '<div>programm</div>'
 })
 ;
Run Code Online (Sandbox Code Playgroud)