I try to make an application with PhoneGap and AngularJS that can be used online and offline (for Android device).
I want to get a list of people from the local database if the device is offline, and from a web service, using $http, if the device is online. But when the device is online, it doesn't work, web service isn't called.
I think the problem is PhoneGap asynchronous method. Indeed, in offline mode, it works but i need to use $scope.$apply to update my view. But that doesn't work for $http...
Does someone know how to use $http in asynchronous method?
function ListCtrl ($scope, $http){
$scope.list = [];
$scope.Id = 2;
$scope.init = function(){
document.addEventListener("deviceready", getList, false);
}
$scope.getAll = function(){
$http({
url: 'http://10.0.0.2:63414/myWebMethod/' + $scope.Id,
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(data) {
$scope.list = data;
});
}
function getList(){
var db = window.openDatabase("Database", "1.0", "list", 200000);
var network = navigator.connection.type;
if (network == "none"){
// local database transaction works fine
} else {
$scope.getAll();
}
$scope.$apply();
}
$scope.init();
}