IONIC: unable to get values out of the `$http` within `$ionicPlatform.on` method

I am using cordova local notification its working fine but i need to send notification on specific condition within $ionicPlatform.ready. For condition, i am getting data from webservice using $http but unfortunately i am unable to retrieve data out of the $http.

$ionicPlatform.ready(function() {
var conditionvariable=“true”;

$http.get(“http://www.example.com/abc.php”).then(function(response)
{
var test = response.data[0];
if(test.result==‘test’)
{
conditionvariable=“false”;
}
});

alert(conditionvariable);
})
alert(conditionvariable); always gives true if condition is true. if i alert it just after if condition its alerts(false) right value.

You need to learn basic asynchronous coding. This isn’t really an ionic or angular issue, just a general how to code in javascript thing. Maybe try here: https://scotch.io/tutorials/javascript-promises-for-dummies

1 Like

thank you so much @rloui for the reply. I have tried to use js promises but no luck. I just want some value which is comes from $http.get function and i have to used it out of the $http.get function.

Declare a variable within the Run method. Assign the $http.get method output to that variable and access it out of $http.get scope.

Absolutely do not do that, that creates crazy global variable spaghetti code.

Here is what you need:

$http.get("http://www.example.com/abc.php")
  .then(function(response) {
    return response.data[0] !== 'test';
  })
  .then(function (isNotTest) {
    alert(isNotTest);
  });

Saying you tried to use js promises but no luck doesn’t make sense, you are actively using promises in your example. The value that comes from $http.get is a promise. You are running into issues because you don’t know how to use them, hence me giving you an article on how to use them.

Thank you so much @rloui i will try this.