2015-05-29 62 views
0

在此創建的嵌套ng-repeat.Now中,我需要將第一個ng-repeat id值傳遞給內部ng-repeat。我怎麼能傳遞val.id在內ng-repeat如何在ng-repeat中傳遞動態範圍值

我的表

<table> 
<tr ng-repeat="val in sample"> 
<td> 
<table> 
    <tr> 
     <td>{{val.id}}</td> 
     <td>{{val.name}}</td> 
    </tr> 
     <tr ng-repeat="new_val in new_sample{{val.id}}"> 
     <td> 
      <table> 
       <tr> 
        <td>{{new_val .new_id}}</td> 
        <td>{{new_val .new_name}}</td> 
       </tr> 
       </table> 
     </td> 
     </tr> 
</table> 
</td> 
</tr> 
</table> 

我的腳本

var myApp = angular.module("myApp", []); 
myApp.controller("MyController", function MyController($scope){ 
    $scope.sample = [ 
     {'id' : '1','name' : 'jon'}, 
     {'id' : '2','name' : 'albert'} 
    ]; 

    $scope.new_sample1 = [ 
     {'id' : '11','name' : 'jon1'}, 
     {'id' : '22','name' : 'albert1'} 
    ]; 

    $scope.new_sample2 = [ 
     {'id' : '111','name' : 'jon2'}, 
     {'id' : '222','name' : 'albert2'} 
    ]; 
}); 

回答

2

您可以在您的NG-repeat元素使用ng-init做到這一點。在這種情況下,您的HTML將是

<table ng-app="myApp" ng-controller="MyController"> 
<tr ng-repeat="val in sample" ng-init="subVal=new_sample[val.id]"> 
<td> 
<table> 
    <tr> 
     <td>{{val.id}}</td> 
     <td>{{val.name}}</td> 

    </tr> 
     <tr ng-repeat="new_val in subVal"> 
     <td> 
      <table> 
       <tr> 
        <td>{{new_val.id}}</td> 
        <td>{{new_val.name}}</td> 
       </tr> 
       </table> 
     </td> 
     </tr> 
</table> 
</td> 
</tr> 
</table> 

並在腳本中。

var myApp = angular.module("myApp", []); 
    myApp.controller("MyController", function MyController($scope){ 
    $scope.sample = [ 
    {'id' : '1','name' : 'jon'}, 
    {'id' : '2','name' : 'albert'} 
    ] 

    $scope.new_sample = { 
     1 : [ 
       {'id' : '11','name' : 'jon1'}, 
       {'id' : '22','name' : 'albert1'} 
      ], 
     2 : [ 
       {'id' : '111','name' : 'jon2'}, 
       {'id' : '222','name' : 'albert2'} 
     ] 

    } 

    }); 

但請注意,您需要更改您的new_sample對象,如上所述。 A working Feedel is here

+0

感謝U Buddy它的工作:-) – Pravin

+0

這是我的榮幸:) – Vineet

0

ng-repeat格應通過其索引&訪問數組,這可能是上可行標記本身。

標記

<tr ng-repeat="new_val in ['new_sample'+ val.id]"> 
+0

@Pravin對您有幫助嗎? –

0

不能使用{{}}在NG-重複訪問對象的名稱。您需要更改對象的結構才能動態訪問它們。

這是幾乎沒有改變。

var myApp = angular.module("myApp", []); 
myApp.controller("MyController", function MyController($scope){ 
     $scope.sample = [ 
      {'id' : '1','name' : 'jon'}, 
      {'id' : '2','name' : 'albert'} 
     ] 

     $scope.innerSample = { 
      "new_sample1" : [ 
       {'id' : '11','name' : 'jon1'}, 
       {'id' : '22','name' : 'albert1'} 
      ], 
      "new_sample2" : [ 
       {'id' : '111','name' : 'jon2'}, 
       {'id' : '222','name' : 'albert2'} 
      ] 
     }; 
}); 

HTML文件

<table> 
     <tr ng-repeat="val in sample"> 
      <td> 
       <table> 
        <tr> 
         <td>{{val.id}}</td> 
         <td>{{val.name}}</td> 
        </tr> 
          <tr ng-repeat="new_val in innerSample[newSample{{val.id}}]"> 
           <td> 
            <table> 
             <tr> 
              <td>{{new_val .new_id}}</td> 
              <td>{{new_val .new_name}}</td> 
             </tr> 
            </table> 
           </td> 
         </tr> 
       </table> 
      </td> 
     </tr> 
</table>