如何为 ng-table 创建自定义指令(对 table 和 ng-repeat 使用不同的数据)
How to create a custom directive for ng-table (using different data for the table and ng-repeat)
我在尝试为 ng-table 创建自定义指令时遇到了一些困难。
在我的应用程序中,我多次使用 table "users"。
我有一个显示所有用户的主要 table,还有一些显示有限数量用户的自定义 table。
tables 的这些自定义数据被创建到控制器中并具有不同的名称(usersList1、usersList2 等)。
table完全相同(header,列标签),只是里面的数据发生了变化。
我想要一个自定义指令并将要显示的数据传递给它。
这是我的代码:
.directive('tableForListingDirective', function () {
return {
restrict: 'EA',
scope: true,
template: [
'<table ng-table="tableParams" class="table table-striped table-bordered table-hover" template-pagination="custom/pager">',
'<tr ng-repeat="user in tableData">',
'<td width="30" style="text-align: left" header="\'ng-table/headers/checkbox.html\'"> <input type="checkbox" ng-model="user.checked" ng-change="toggleCheckedUserToAdd(user)"/>',
'<td data-title="\'Status\'" sortable="\'status\'">{{user.status}}</td>',
'<td data-title="\'Login\'" sortable="\'login\'">{{user.login}}</td>',
'<td data-title="\'Name\'" sortable="\'name\'">{{user.name}}</td>',
'<td data-title="\'Email\'" sortable="\'email\'">{{user.email}}</td>',
'</tr>',
'</table>'
].join(''),
link: function (scope, element, attrs) {
scope.tableData=scope.$eval(attrs.tableData);
},
}
});
在html文件中调用:
<table-for-listing-directive table-data="{{users}}"></table-for-listing-directive>
你可能会说,我为什么不把 tableData: "=" 放入作用域。
好吧,经过几个小时的搜索,我发现 ng-repeat 和 scope{} 不想相互合作,所以为了让 ng-repeat 在指令中工作,我必须说 'scope: true'.
在ng-repeat里面我试过ng-repeat="user in {{tableData}}"
也没用。
在您的 HTML 中将 table-data="{{users}}"
更改为 table-data="users"
我在尝试为 ng-table 创建自定义指令时遇到了一些困难。 在我的应用程序中,我多次使用 table "users"。 我有一个显示所有用户的主要 table,还有一些显示有限数量用户的自定义 table。 tables 的这些自定义数据被创建到控制器中并具有不同的名称(usersList1、usersList2 等)。
table完全相同(header,列标签),只是里面的数据发生了变化。
我想要一个自定义指令并将要显示的数据传递给它。
这是我的代码:
.directive('tableForListingDirective', function () {
return {
restrict: 'EA',
scope: true,
template: [
'<table ng-table="tableParams" class="table table-striped table-bordered table-hover" template-pagination="custom/pager">',
'<tr ng-repeat="user in tableData">',
'<td width="30" style="text-align: left" header="\'ng-table/headers/checkbox.html\'"> <input type="checkbox" ng-model="user.checked" ng-change="toggleCheckedUserToAdd(user)"/>',
'<td data-title="\'Status\'" sortable="\'status\'">{{user.status}}</td>',
'<td data-title="\'Login\'" sortable="\'login\'">{{user.login}}</td>',
'<td data-title="\'Name\'" sortable="\'name\'">{{user.name}}</td>',
'<td data-title="\'Email\'" sortable="\'email\'">{{user.email}}</td>',
'</tr>',
'</table>'
].join(''),
link: function (scope, element, attrs) {
scope.tableData=scope.$eval(attrs.tableData);
},
}
});
在html文件中调用:
<table-for-listing-directive table-data="{{users}}"></table-for-listing-directive>
你可能会说,我为什么不把 tableData: "=" 放入作用域。 好吧,经过几个小时的搜索,我发现 ng-repeat 和 scope{} 不想相互合作,所以为了让 ng-repeat 在指令中工作,我必须说 'scope: true'.
在ng-repeat里面我试过ng-repeat="user in {{tableData}}"
也没用。
在您的 HTML 中将 table-data="{{users}}"
更改为 table-data="users"