ES6 angular-meteor ng-table 未调用 getData 函数
ES6 angular-meteor ng-table getData function not called
我正在尝试将我的代码重构为 ES6。我正在使用 angular-meteor 和 ng-table。重构前,数据显示在 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);
正在填充 const data
,但未调用 getData
。模板中的 table 使用 ctrl.itemTableParams
作为 ng-table
属性的值,它的 ng-repeat
是 item in $data
.
有人知道为什么 getData
函数没有被调用吗?帮助将不胜感激。谢谢!
P.S。
当我尝试将 NgTableParams
设置为 const tableParams
,然后调用 reload()
函数时,触发了 getData
。但问题是,它没有在 table 上呈现数据。我将 table 设置为:
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
中记录数据时,其中包含项目。但是,正如我所说,它没有在 table.
中呈现
getData
方法未被调用,因为您异步获取 data
但同步使用它。因此,当最初加载控制器时,getData
会使用未解析的数据调用。
要解决此问题,您需要在 data
对象的成功回调中创建 NgTableParams
:
data.$promise.then((data) => {
// create NgTableParams here
});
显然,您只需要 return getData
中的数据。旧文档使用 $defer.resolve
而不是 return 解析数据。当前版本 (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;
}
});
}
});
我正在尝试将我的代码重构为 ES6。我正在使用 angular-meteor 和 ng-table。重构前,数据显示在 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);
正在填充 const data
,但未调用 getData
。模板中的 table 使用 ctrl.itemTableParams
作为 ng-table
属性的值,它的 ng-repeat
是 item in $data
.
有人知道为什么 getData
函数没有被调用吗?帮助将不胜感激。谢谢!
P.S。
当我尝试将 NgTableParams
设置为 const tableParams
,然后调用 reload()
函数时,触发了 getData
。但问题是,它没有在 table 上呈现数据。我将 table 设置为:
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
中记录数据时,其中包含项目。但是,正如我所说,它没有在 table.
getData
方法未被调用,因为您异步获取 data
但同步使用它。因此,当最初加载控制器时,getData
会使用未解析的数据调用。
要解决此问题,您需要在 data
对象的成功回调中创建 NgTableParams
:
data.$promise.then((data) => {
// create NgTableParams here
});
显然,您只需要 return getData
中的数据。旧文档使用 $defer.resolve
而不是 return 解析数据。当前版本 (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;
}
});
}
});