这是一个典型的不理解JS承诺的顺序或操作的例子。你的
ngTable
在数据返回之前调用初始化。
问题是你的电话
ngTableParams()
直接在
server.get
打电话。在控件移动到
.then()
new ngTableParams()
调用一个函数,并在服务器调用完成后调用它。这是构建代码的正确方法:
function initialize() {
c.server.get({
action: 'retrieve_data'
}).then(function(response) {
//Set value to hide loading spinner
c.data.loading = false;
//Get link data
c.onbCase = response.data.onbCase;
// don't run the ngTable init until the http call has returned
ngTableInit();
});
}
initialize();
//wrap your ngTableParams constructor in a function
function ngTableInit() {
$scope.usersTable = new ngTableParams({
page: 1,
count: 5
}, {
total: c.onbCase.length,
getData: function ($defer, params) {
...
}
});
}
如果在原始代码中在浏览器的调试器中设置断点,您将看到这一点
$scope.usersTable = new ngTableParams({...})
c.onbCase
未定义。