ES6 angular-meteor ng-table getData函数未被调用
ES6 angular-meteor ng-table getData function not called
我正在尝试重构我的代码到ES6。我用的是角流星和ng表。在重构之前,数据显示在表中。然而,在重构到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
没有被调用。模板表中ng-table
属性值为ctrl.itemTableParams
, 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
中记录数据时,它在其中有项目。但是,就像我说的,它不是在表格中呈现的
显然,您只需要返回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;
}
});
}
});
getData
方法没有被调用,因为您异步获取data
,但同步使用它。因此,当初始加载控制器时,getData
被调用,并带有未解析的数据。
要解决这个问题,您需要在data
对象的成功回调中创建NgTableParams
:
data.$promise.then((data) => {
// create NgTableParams here
});
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量