Ionic Exit APP not working - angularjs

I'm trying to set ng-click="ionic.Platform.exitApp()" to a button but it dosen't work, i also tried ng-click="exitApp()" and ng-click="$ionic.Platform.exitApp()"but it dosen't work either.
I'm using Ionic Creator, according to this docs http://ionicframework.com/docs/api/utility/ionic.Platform/ it should be quite simple, but i don't know how to correctly set this adjust. I also tryied to add a module into Ionic Creator as:
angular.module('app.exit', []).exit(function($scope) {
ionic.Platform.ready(function(){
// will execute when device is ready, or immediately if the device is already ready.
});
var deviceInformation = ionic.Platform.device();
var isWebView = ionic.Platform.isWebView();
var isIPad = ionic.Platform.isIPad();
var isIOS = ionic.Platform.isIOS();
var isAndroid = ionic.Platform.isAndroid();
var isWindowsPhone = ionic.Platform.isWindowsPhone();
var currentPlatform = ionic.Platform.platform();
var currentPlatformVersion = ionic.Platform.version();
ionic.Platform.exitApp(); // stops the app
});
Adding this module console log prints: "exit is not a function"
Thanks

You can always try to use this ExitApp Plugin
To use, add the plugin by cordova plugin add cordova-plugin-exitapp and then add the following code to your app.js
angular.module('XYZApp', ['ionic'])
.run(function($ionicPlatform, $ionicHistory, $rootScope) {
$ionicPlatform.ready(function() {
// For exit on back button press
$ionicPlatform.registerBackButtonAction(function(e) {
if ($rootScope.backButtonPressedOnceToExit) {
navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
// "Press back button again to exit" : show toast
setTimeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
}
e.preventDefault();
return false;
}, 101);
});
}

Thanks Chiragh Dewan,
But it dosen't work either, i added this:
angular.module('XYZApp', []).run(function($ionicPlatform, $ionicHistory, $rootScope) {
$ionicPlatform.ready(function() {
// For exit on back button press
$ionicPlatform.registerBackButtonAction(function(e) {
if ($rootScope.backButtonPressedOnceToExit) {
navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
// "Press back button again to exit" : show toast
setTimeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
}
e.preventDefault();
return false;
}, 101);
});
}
);
... into a new JS file called exit.js
Then i added the module XYZApp into the Ionic Creator code settings Angular modules
The last step i added the cordova-plugin-exitapp
I tryied ng-click="navigator.app.exitApp()" and ng-click="ionic.Platform.exitApp()" and none of them seems to work.
U can see the project here: https://creator.ionic.io/share/html/2d9b0126751e#/menu/contacto
Thanks

Related

angularjs: $interval call never happen after it's cancelled

I've a requirement where as soon as i load a page it makes makes api call and
shows data on the page. and after every 15 sec it keeps loading the data an shows and i've a popup that has a click button function(ConfigModalButtonClick ). When i open the popup i wanted to stop the interval that makes the api call so for that i added $interval.cancel when popup is open but now when i click the button in popup the api call never happen. how can i make sure api call happens it looks like i need to call interval function again somewhere but not sure where??
the interval should run normally after 15 sec when i close the popup.
$scope.timeDelay = 15000;
$scope.loadData = function() {
$http.post(url,data
).then(
function(response) {
$scope.output = response.data;
},
function(response) {
$scope.retrieveErrorMssg = "Failed to retrieve required data. Try again.";
});
};
var interval = $interval(function() {
$scope.loadData();
}, $scope.timeDelay);
$scope.showConfigurationModal = function() {
$interval.cancel(interval);
$scope.state.settingModalVisible = true;
};
$scope.closeConfigurationModal = function() {
$scope.state.settingModalVisible = false;
// Need to explicitly trigger the dismiss function of the modal
$scope.settingModalDismiss();
};
$scope.ConfigModalButtonClick = function () {
$scope.state.settingModalVisible = false;
//some data manipulation
$scope.loadData();
};

$scope.$on('element') watchers are not being triggered after $scope.element is set

This is a controller where I am getting some data from firebase and updating my scope, then I want to do some more stuff once that scope is updated:
.controller('loginCtrl', ['$scope','$firebaseAuth','$firebaseObject','$location',
'localStorageService','$q',
function($scope,$firebaseAuth,$firebaseObject,$location,
localStorageService,$q) {
// Called
$scope.googleAuth = function(){
var auth = $firebaseAuth();
auth.$signInWithPopup("google").then(function(firebaseUser){
// Google approved
// Step 1. Get user profile
getProfile(firebaseUser);
}).catch(function(error){
console.log("Authentication failed:",error);
$scope.errorMsg = "Something went wrong. Please try again."
});
}
// Watchers
$scope.$on('profile',function(){
console.info("REACH");
if($scope.profile == null){ // User doesn't exist
console.info("profile null");
if(isAssigned(firebaseUser) != null){
// Redirect to create a profile page
} else {
// Do not let them in TODO: adjust the text
$scope.erorrMsg = "It seems you don't have a profile. Please contact us on ----------- to get a profile";
}
} else {
// Step 2. Get user parent info
// getParent($scope.profile.current_centre_id,$scope.profile.refer_id);
console.info("Getting parent");
getParent('-KeVj_sr-uCY3zx0kbb6','-KeVpUA4OIK4msNIl0gQ');
}
});
// After getting parent info
$scope.$on('parent',function(){
localStorageService.set('currentUser',{
profile: $scope.profile,
parent: $scope.parent
});
});
var getProfile = function(firebaseUser){
var email = firebaseUser.user.email;
var ref = firebase.database().ref();
// Getting profile
$scope.profile = $firebaseObject(ref.child('profiles').orderByChild('email').equalTo('dev.beezbutt#gmail.com'));
}
var getParent = function(centre_id,parent_id){
$scope.parent = $firebaseObject(ref.child('parents/'+centre_id+'/'+parent_id));
}
var isAssigned = function(firebaseUser){
// TODO
return false;
}
}]);
$scope.$on('profile') & $scope.$on('parent') are not being triggered, and I'm not sure why or what am I missing.
I know that $scope.profile is being set because I'm printing it in html.
Instead of $scope.$on, use $scope.$watch to watch the scope variables.
$scope.$watch("scope_variable_name", function(newVal, oldVal){
// Add your code here
});

interstitial ad don't show up (AngularJs)

my app.js :
$ionicPlatform.ready(function() {
var admobid = {};
// select the right Ad Id according to platform
if( /(android)/i.test(navigator.userAgent) ) {
admobid = { // for Android
interstitial: 'ca-app-pub-3940256099942544/1033173712'
};
} else {
admobid = { // for Windows Phone
interstitial: 'ca-app-pub-3940256099942544/1033173712'
};
}
if(window.AdMob) AdMob.prepareInterstitial({
adId:admobid.interstitial,
isTesting:true,
autoShow:false
});
my controller.js :
.controller('ChatsCtrl', function($scope, Chats) {
$scope.$on('$ionicView.beforeEnter', function(e) {
if (window.AdMob) AdMob.showInterstitial();
});
$scope.$on('$ionicView.beforeLeave', function(e) {
if (window.AdMob) AdMob.showInterstitial();
});
})
I'm building an ionic framework app for android and i need to add it a interstitial ad, it should run whenever user enters chats tab.
Here is my code, i copied most part of it from http://pointdeveloper.com/how-to-add-interstitial-ads-on-navigation-to-ionic-framework-apps/ .
I changed interstitial: 'ca-app-pub-3940256099942544/1033173712' parts with my own key but it didn't work. Where is the problem?
(I'm testing it in both my android phone(Nexus 5) and Genymotion android emulator)
The internal life cicle for interstitial ads when requestInterstitialAd is called is the following one, you can read more here: https://github.com/appfeel/admob-google-cordova/wiki/requestInterstitialAd
requestInterstitialAd();
options.autoShowInterstitial == true ? showInterstitial : raise(admob.events.onAdLoaded)
So if you have set options.autoShowInterstitial = true, then it should be automatically shown, otherwiese, you should call admob.requestInterstitialAd and wait for admob.events.onAdLoaded with atType === 'interstitial' to be raised. Then you should do something like:
document.addEventListener(admob.events.onAdLoaded, function (e) {
if (e.adType === admob.AD_TYPE.INTERSTITIAL) {
admob.showInterstitialAd();
}
});

Firebase function not running until view change [duplicate]

This question already has an answer here:
How come Angular doesn't update with scope here?
(1 answer)
Closed 6 years ago.
I have a Firebase function inside an angular controller. There is a button that when clicked takes the selected option and the type input and stores them into the user's object like so:
{
selected-option : input-value
}
This works perfectly, but only works when the view is changed. In my case both airlines already have data so this function displays an $ionicPopup.
After the view has changed once the functionality is absolutely perfect. This is obviously a problem and I assume it is an $apply or $digest issue.
Here is my controller code (Supected location marked by "ISSUE RIGHT HERE"):
.controller('ProgramCtrl', ['$scope', '$state', '$firebaseArray', 'facebook', '$ionicPopup', '$ionicLoading',
function($scope, $state, $firebaseArray, facebook, $ionicPopup, $ionicLoading) {
$scope.goBack = function() {
$state.go('app.home');
}
$scope.show = function() {
$ionicLoading.show({
template: 'Loading...'
});
};
$scope.hide = function(){
$ionicLoading.hide();
};
// Get who is logged in
$scope.user = facebook.get();
// Array of airlines
var airRef = ref.child("airlines");
$scope.airlines = $firebaseArray(airRef);
$scope.selectedAir = {};
$scope.miles = {};
$scope.revealInput = function(num) {
// Jquery variables
$milesPoints = $(".milesPoints");
$saveTicket = $(".saveTicket");
// Will fade in visibility depending on num passed into function
switch(num) {
case 1:
$saveTicket.prop("disabled", false);
$saveTicket.fadeTo(400, 1);
break;
default:
break;
}
}
// Add program to user
$scope.addProgram = function () {
// Connect to Firebase
Firebase.goOnline();
// Check for facebook user
if(jQuery.isEmptyObject($scope.user)) {
// Get Firebase user
var authData = ref.getAuth();
var theUser = ref.child("users").child(authData.uid);
var selected = {};
// Access user miles data
// $scope.show();
// ISSUE RIGHT HERE
theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
// Update scopes
var exist = snapshot.exists();
// Check if object id exists, if so notify user
if(!exist) {
// Save and update program to user object
selected[$scope.selectedAir.name.$id] = $scope.miles.num;
theUser.child("miles").update(selected);
//$scope.hide();
$state.go("app.saved");
} else {
// Popup alert
var alertPopup = $ionicPopup.alert({
title: 'Oops!',
template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
});
alertPopup.then(function(res) {
console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
});
}
})
} else {
var theUser = ref.child("users").child($scope.user.id);
var selected = {};
$scope.show();
theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
var exist = snapshot.exists();
if(!exist) {
selected[$scope.selectedAir.name.$id] = $scope.miles.num;
theUser.child("miles").update(selected);
$scope.hide();
$state.go("app.saved");
} else {
var alertPopup = $ionicPopup.alert({
title: 'Oops!',
template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
});
alertPopup.then(function(res) {
console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
});
}
})
}
}
}])
Thanks for the help and I can provide more code or screenshots if needed.
The issue is in this piece of code:
theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
$timout(function() {
var exist = snapshot.exists();
// Check if object id exists, if so notify user
if(!exist) {
// Save and update program to user object
selected[$scope.selectedAir.name.$id] = $scope.miles.num;
theUser.child("miles").update(selected);
//$scope.hide();
$state.go("app.saved");
} else {
// Popup alert
var alertPopup = $ionicPopup.alert({
title: 'Oops!',
template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
});
alertPopup.then(function(res) {
console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
});
}
});
})
When you call once(), it starts loading data from Firebase. Since this may take some time, you pass in a callback function that is invoked when the data is available. But by the time the callback function is invoked, AngularJS is not expecting updates to the $scope anymore.
The solution is to wrap the code into a $timeout(), which ensures it gets executed when AngularJS is ready to handle scope changes again:
theUser.child("miles").child($scope.selectedAir.name.$id).once("value", function(snapshot) {
// Update scopes
var exist = snapshot.exists();
// Check if object id exists, if so notify user
if(!exist) {
// Save and update program to user object
selected[$scope.selectedAir.name.$id] = $scope.miles.num;
theUser.child("miles").update(selected);
//$scope.hide();
$state.go("app.saved");
} else {
// Popup alert
var alertPopup = $ionicPopup.alert({
title: 'Oops!',
template: "You already created this airline! Go to the 'Add Ticket' page to add more points."
});
alertPopup.then(function(res) {
console.log("You already created this airline! Go to the 'Add Ticket' page to add more points.");
});
}
})
Note that this problem wouldn't happen if you used AngularFire's $firebaseObject() and $firebaseArray() primitives, since those automatically notify AngularJS of scope changes.
We get this question a lot. Here's a recent one: Taking long to load

Why does Angular JS count wrong clicks?

I have Angular JS code:
$scope.click = function (id) {
notificationsModal.confirm(function () {
$scope.$apply(function () {
console.log(id);
if(id == "test"){
$scope.contacts.blocked = $scope.contacts.blocked + 1;
}
});
}
}
When I do click I get confirm JS window with cancel button. I push this button for canceling event. I do this operation 4 ones.
After I confirm message and get 4 outputs console.log(id); in console. Why does it work for each click if I did cancel and condition had not been executed?

Resources