【发布时间】:2016-08-21 21:09:58
【问题描述】:
我对 Angular 还是很陌生,所以也许这不是解决问题的最佳方法。我正在尝试从名为 topDevices 的指令范围内访问名为 Devices 的工厂。
topDevices.js:
app.directive('topDevices', function() {
return {
restrict: 'E',
scope: {
count: '@',
sortKey: '@',
devices: Devices.sortByKey(this.count, this.sortKey)
},
templateUrl: 'app/directives/topDevices.tpl.html'
}
});
这是通常不允许的事情还是只是不好的做法/方法? Devices 包含使用 Devices.all() 的设备列表,但我还有一个 Devices.sortByKey(count, key) 函数来返回按特定键排序的有限设备集。
编辑:更多信息
这样做的目的是创建一个模板,例如,可以按某个 X 指标列出前 5 个设备。模板是这样的:
<h3>Top {{ count }} by {{ sortKey }}</h3>
<table class="table table-bordered table-condensed table-striped table-hover">
<tbody>
<tr ng-repeat="device in devices">
<td>{{ device.id }}</td>
<td>{{ device.name }}</td>
<td>{{ device[sortKey] }}</td>
</tr>
<tr ng-show="!devices.length">
<td colspan="3">No device info available</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: javascript angularjs