2016-03-04 586 views
2

我需要在我的頁面上有兩個表格和表格中的很多元素。將參數傳遞給Angular的ng -include

我創建的文件AccountItem.html:

<div data-as-sortable-item-handle> 
<table> 
    <tr> 
     <td class="draggable-index-field"> 
      {{$index + 1}} 
     </td> 
     <td class="draggable-name-field"> 
      {{item.accountName}} 
     </td> 
     <td class="draggable-enabled-field"> 
      <input id="enabled-{{$index}}" type="checkbox" name="accounts[]" ng-checked="item.enabled" disabled/> 
     </td> 
    </tr> 
</table> 

此文件與表的每一行。

我也創建了表SortAccountList.html文件:

<div class="panel panel-info"> 
<div class="panel-heading"> 
    <h3 class="panel-title ng-binding">{{title}}</h3> 
</div> 
<div class="panel-body"> 
    <table> 
     <td class="draggable-index-field"> 
      # 
     </td> 
     <td class="draggable-name-field"> 
      Account Name 
     </td> 
     <td class="draggable-enabled-field"> 
      Enabled 
     </td> 
    </table> 
    <ul data-as-sortable = "sortableOptions" data-ng-model="accountList" class="draggable-area"> 
     <li ng-repeat="item in accountList" data-as-sortable-item ng-include="'AccountItem.html'"></li> 
    </ul> 
</div> 

我包括與指令該文件在我的主文件:

<div ng-include="'SortAccountList.html'" onload="title = 'Sorted Account List'; accountList = sortedAccounts"></div> 

當我加載瀏覽器這個頁面它顯示了我的表格,標題,我通過「標題」參數傳遞給它。但它不想在表格中顯示行。但是,如果我更改accountListsortedAccounts這是我的控制器中的列表它顯示所有行。 我無法使用sortedAccounts,因爲我有第二張表,我想從我的控制器傳遞不同的帳戶。

所以,我的問題,如何正確傳遞這個參數?謝謝!

回答

4

include標籤上的ng-init =「」屬性應該做你需要的。

<div ng-include="'SortAccountList.html'" 
ng-init="title = 'SortedAccount List'; accountList = sortedAccounts"></div>` 
+0

不幸的是,這並沒有幫助。我可以在表格中看到標題,但也沒有行 – migAlex

0

請勿使用ngInclude。使用directive並使用指令的範圍傳遞數據。

directive('sortAccountList', function() { 
    return { 
    restrict: 'E', 
    scope: { 
     title: '=', 
     accountList: '=' 
    }, 
    templateUrl: 'SortAccountList.html' 
    }; 
});