dor*_*ork 12 javascript angularjs ecmascript-6 ngtable angular-meteor
我正在尝试将我的代码重构为ES6.我正在使用角度流星和ng-table.在重构之前,数据显示在表格中.但是,在重构为ES6语法后,数据不再显示.这是重构代码的片段:
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);
Run Code Online (Sandbox Code Playgroud)
该const data是越来越填充,但getData不获取调用.模板中的表ctrl.itemTableParams用作ng-table属性的值,它ng-repeat是item in $data.
有没有人知道为什么getData不调用这个函数?非常感谢帮助.谢谢!
PS当我尝试设置NgTableParams为a 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>
Run Code Online (Sandbox Code Playgroud)
当我记录数据时getData,它中有项目.但是,就像我说的那样,它并没有在表格中呈现.
显然,你只需要返回getData. 旧文档正在使用$defer.resolve并且没有返回解析的数据。当前版本(1.0.0)不再使用它。
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) => {
const filteredData = filterData(data); // do something
return filteredData;
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
371 次 |
| 最近记录: |