【发布时间】:2016-11-04 15:59:05
【问题描述】:
场景:我有一个工厂服务,代码如下
appServices.factory('MyDB', function ($cordovaSQLite)
{
var FixedCache = [];
return {
getFromCache: function (dataURL, $scope)
{
var query = "SELECT * FROM FixedCache WHERE URI_ID = '"+dataURL+"'";
FixedCache = [];
$scope.dataFromCache = [];
//console.log("Inside Cache method");
// Execute query statement from query variable.
$cordovaSQLite.execute(db, query).then(function (res)
{
if (res.rows.length > 0)
{
var dataItem = {
id : res.rows.item(0).id ,
URI_ID : res.rows.item(0).URI_ID ,
ExpireDate : res.rows.item(0).ExpireDate ,
JSONData : res.rows.item(0).JSONData
};
FixedCache.push(dataItem);
$scope.dataFromCache = FixedCache;
}
});
return FixedCache;
},// End select all data.
};
})
在控制器中,我想通过调用这个方法来获取数据。这是控制器的代码。
appControllers.controller('categoriesCtrl', function ($scope, $http, $state, $timeout, $stateParams, $ionicHistory, MyDB, UtilityMethods) {
var dataURL = window.globalVariable.constants.URL_API;
$scope.initialForm = function () {
$scope.categoriesFromCache = [];
$scope.categoryCache;
$scope.url = window.globalVariable.constants.URL_API;
$scope.todaysDate = UtilityMethods.GetDate(0);
// $scope.isLoading is the variable use for loading.
$scope.isLoading = false;
$scope.apiUrl = window.globalVariable.constants.URL_API;
};// End initialForm.
$scope.GetFromCache = function (uri, $scope) {
$scope.dataFromCache = [];
MyDB.getFromCache(uri, $scope);
};
$scope.navigateTo = function (targetPage, objectData, apiUrl) {
$state.go(targetPage, {
category: objectData,
apiUrl: apiUrl
});
};
$scope.clearSearch = function() {
$scope.searchByName = '';
};
$scope.goBack = function () {
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("app.categories", {
isShowError: false
});
};
$scope.initialForm();
$timeout(function () {
$scope.GetFromCache(dataURL, $scope);
}, 3000);// End loading progress.
});
问题: 当我调用这个方法 MyDB.getFromCache(uri, $scope);在控制器中,它返回空数组 [] 数据库确实包含数据。
请指导我。我是否正确调用函数?
【问题讨论】:
标签: javascript angularjs sqlite ionic-framework factory