如何在两个阵列AngularJs Ionic上重复

时间:2021-12-14 19:55:42

I have i side menu with accordion under menu.

我有菜单下的手风琴菜单。

i populate those under menus with my controller so i did this :

我使用我的控制器填充菜单下的那些,所以我这样做:

controller.js :

$scope.groupsR = [];
for (var t=0; t<1; t++) {
  $scope.groupsR[t] = {
    name: "Reporting",
    titles: [],
    urls: []
  };
  for (var u=0; u<2; u++) {
    switch (u){
      case 0 : $scope.groupsR[t].titles.push("Pilotage Commercial");
               $scope.groupsR[t].urls.push("pilotageCommercial");
        break;
      case 1 : $scope.groupsR[t].titles.push("Pilotage BackOffice");
               $scope.groupsR[t].urls.push("pilotageBackOffice");
        break;
    }
  }
}

menu.html :

<ion-item class="item-accordion"
                ng-repeat="title in groupR.titles"
                ng-repeat = "url in groupR.urls"
                ng-show="isGroupShown(groupR)"
                ui-sref="menu.{{url}}"
                menu-close="">
        {{title}}
</ion-item>

the under menu appear with the correct title but the url are wrong ..

下面的菜单显示正确的标题,但网址错误..

1 个解决方案

#1


2  

Use the index from the first ng-repeat:

使用第一个ng-repeat中的索引:

{{ groupR.urls[$index] }}


Edit: Better yet:

switch (u){
    case 0: angular.extend($scope.groupsR[t], {
        title: "Pilotage Commercial",
        url: "pilotageCommercial"
    });
    break;
    case 1: angular.extend($scope.groupsR[t], {
        title: "Pilotage BackOffice",
        url: "pilotageBackOffice"
    });
    break;
}

with:

<ion-item class="item-accordion"
    ng-repeat="item in groupR"
    ng-show="isGroupShown(groupR)"
    ui-sref="menu.{{item.url}}"
    menu-close="">
    {{item.title}}
</ion-item>

This also works when you use filtering or sorting, because that will mess with the $index variable

这在使用过滤或排序时也有效,因为这会使$ index变量混乱

#1


2  

Use the index from the first ng-repeat:

使用第一个ng-repeat中的索引:

{{ groupR.urls[$index] }}


Edit: Better yet:

switch (u){
    case 0: angular.extend($scope.groupsR[t], {
        title: "Pilotage Commercial",
        url: "pilotageCommercial"
    });
    break;
    case 1: angular.extend($scope.groupsR[t], {
        title: "Pilotage BackOffice",
        url: "pilotageBackOffice"
    });
    break;
}

with:

<ion-item class="item-accordion"
    ng-repeat="item in groupR"
    ng-show="isGroupShown(groupR)"
    ui-sref="menu.{{item.url}}"
    menu-close="">
    {{item.title}}
</ion-item>

This also works when you use filtering or sorting, because that will mess with the $index variable

这在使用过滤或排序时也有效,因为这会使$ index变量混乱