2016-03-04 584 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' 
    }; 
});