$scope.obj_qst_local_study_main = tbl_qst_local_study_main.all();
$scope.quesion_id_allocated = $scope.obj_qst_local_study_main[0];
$timeout(function(){
console.log('----------all objects----------');
console.log($scope.obj_qst_local_study_main);
console.log('-----------one object-----------');
console.log($scope.quesion_id_allocated);
},200);
当我使用时:
$scope.obj_qst_local_study_main[0];
结果是:
undefined
我的angularjs服务:
.service('tbl_qst_local_study_main', function($cordovaSQLite, DATABASE_LOCAL_NAME){
var self = this;
var qst_local_study_main_array = [];
self.all = function() {
var db = $cordovaSQLite.openDB({name: DATABASE_LOCAL_NAME,location:'default'});
$cordovaSQLite.execute(db, "SELECT * FROM qst_local_study_main")
.then(function (res) {
console.log('--------Successfully read from qst_local_study_main---------');
for (var i = 0; i < res.rows.length; i++) {
qst_local_study_main_array.push(res.rows.item(i));
}
},
function (err) {
console.log(err);
});
return qst_local_study_main_array;
};
})
最佳答案
您的服务应返回Promise
。这是一个超常见的情况,因为(请不要冒犯)人们不理解Promises
的工作方式。
请在互联网上搜索这样的文章:https://developers.google.com/web/fundamentals/getting-started/primers/promises
tl; dr您的服务应返回Promise
。对于您的情况$cordovaSQLite.execute
然后,可以通过链接then
正确处理响应。您也不需要超时。在这里使用超时非常糟糕!
tbl_qst_local_study_main.all()
.then(function(result) {
console.log(result);
})
关于javascript - 如何在angularjs中读取数组对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40212600/