【发布时间】:2017-01-20 23:57:35
【问题描述】:
我正在尝试将我的代码重构为 ES6。我正在使用 angular-meteor 和 ng-table。在重构之前,数据显示在表格中。但是,在重构为 ES6 语法后,数据不再显示。这是重构代码的sn-p:
class MyController {
constructor($scope, $reactive, NgTableParams, MyService) {
'ngInject';
$reactive(this).attach($scope);
this.subscribe('myCollection');
this.myService = MyService;
this.helpers({
items() {
return this.myService.getItems();
},
itemTableParams() {
const data = this.getReactively('items');
return new NgTableParams({
page: 1,
count: 10
}, {
total: data.length,
getData: (params) => {
// not called
}
});
}
});
}
}
class MyService {
getItems() {
return MyCollection.find({}, {
sort: {
dateCreated: -1
}
});
}
}
export default angular.module('MyModule', [angularMeteor, ngTable, MyService])
.component('MyComponent', {
myTemplate,
controllerAs: 'ctrl',
controller: MyController
})
.service('MyService', MyService);
const data 正在填充,但 getData 没有被调用。模板中的表格使用ctrl.itemTableParams 作为ng-table 属性的值,其ng-repeat 为item in $data。
有人知道为什么不调用getData 函数吗?帮助将不胜感激。谢谢!
附:
当我尝试将NgTableParams 设置为const tableParams,然后调用reload() 函数时,会触发getData。但问题是,它不会在表格上呈现数据。我将表格设置为:
itemTableParams() {
const data = this.getReactively('items');
const tableParams = new NgTableParams({
page: 1,
count: 10
}, {
total: data.length,
getData: (params) => {
}
});
tableParams.reload(); // triggers the getData function
return tableParams;
}
<table ng-table="ctrl.itemTableParams">
<tr ng-repeat="item in $data track by $index">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.dateCreated}}</td>
</tr>
</table>
当我在getData 中记录数据时,其中包含项目。但是,就像我说的,它没有在表格中呈现。
【问题讨论】:
-
控制台中是否出现任何错误?您的
items方法调用this.myService.getItems(),但服务有一个方法getInputs,而不是getItems。这是在 SO 问题中的错字还是在您的实际应用中也是如此? -
@noppa 抱歉,这是问题中的拼写错误,而不是应用程序。
标签: javascript angularjs ecmascript-6 ngtable angular-meteor