Javascript ES6角流星ng表getData函数未调用
我正在尝试将代码重构为ES6。我用的是角流星和ng表。重构之前,数据显示在表中。然而,在重构到ES6语法后,数据不再显示。这是重构代码的一个片段:Javascript ES6角流星ng表getData函数未调用,javascript,angularjs,ecmascript-6,ngtable,angular-meteor,Javascript,Angularjs,Ecmascript 6,Ngtable,Angular Meteor,我正在尝试将代码重构为ES6。我用的是角流星和ng表。重构之前,数据显示在表中。然而,在重构到ES6语法后,数据不再显示。这是重构代码的一个片段: class MyController { constructor($scope, $reactive, NgTableParams, MyService) { 'ngInject'; $reactive(this).attach($scope); this.subscribe('myColl
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
为$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>
itemTableParams(){
const data=this.getreactive('items');
const tableParams=新的NgTableParams({
页码:1,
计数:10
}, {
总计:data.length,
getData:(参数)=>{
}
});
tableParams.reload();//触发getData函数
返回表参数;
}
{{item.id}
{{item.name}
{{item.dateCreated}
当我在getData
中记录数据时,其中包含项目。但是,正如我所说,它没有在表中呈现。没有调用getData
方法,因为您异步获取数据,但同步使用它。因此,当控制器最初加载时,将使用未解析的数据调用getData
要解决此问题,您需要在数据
对象的成功回调中创建NgTableParams
:
data.$promise.then((data) => {
// create NgTableParams here
});
显然,您只需要在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;
}
});
}
});
这对角流星有效吗?我尝试过这样做,但是$promise
未定义。您在控制台中有任何错误吗?您的items
方法调用this.myService.getItems()
,但该服务有一个方法getInputs
,而不是getItems
。这是问题中的一个输入错误,还是在你的实际应用程序中也是如此?@noppa抱歉,这是问题中的一个输入错误,而不是应用程序中的输入错误。