So I have a controller that calls a REST service upon loading. Here are the codes:
Controller:
$ionicPlatform.ready(function() { spinnerFactory.showSpinner("Fetching Data");
$http.get("<REST Resource here...>") .success(function(data) { $scope.data = data; }) .error(function(data) { ErrorFactory.getPopup(); }) .finally(function($ionicLoading) { spinnerFactory.hideSpinner(); }); })
Service:
> .factory('spinnerFactory', function($ionicLoading) {
> function showSpinner(info) {
> return $ionicLoading.show({
> template: '<p>' + info +'...</p><ion-spinner></ion-spinner>'
> });
> }
> function hideSpinner() {
> return $ionicLoading.hide();
> }
> return {
> showSpinner: showSpinner,
> hideSpinner: hideSpinner
> };
> })
.factory('ErrorFactory', function($ionicPopup, $state){ function addPopup() { return $ionicPopup.alert({ title: 'Error', subTitle: 'Failed to add data. Please check your internet connection and try again.', cssClass: 'button-assertive' }).then(function(res) { $state.go("menu.home"); }) }
function getPopup() { return $ionicPopup.alert({ title: 'Error', subTitle: 'Failed to fetch data from server. Please check your internet connection and try again.', }).then(function(res) { //.then is when you click the OK button //will reload the current page you're in whenever you click the OK button $state.reload(); }); }
function updatePopup() { return $ionicPopup.alert({ title: 'Error', subTitle: 'Failed to update. Please check your internet connection and try again.', }).then(function(res) { $state.go("menu.home"); }) }
function deletePopup() { return $ionicPopup.alert({ title: 'Error', subTitle: 'Failed to remove selected data. Please check your internet connection and try again.', }).then(function(res) { $state.go("menu.home"); }) }
return { addPopup: addPopup, getPopup: getPopup, updatePopup: updatePopup, deletePopup: deletePopup }; })
When I remove the $ionicLoading everything works fine. I click on the OK buton in the getPopup() alert box and the page will just refresh.
But if the $ionicLoading is included, the getPopup() alert box will fire more than once.