2017-02-25 137 views
-2

我是ionic1框架的新手,并且在sidemenu离子应用上工作。我正在使用cordova local notification其工作正常,但我需要在$ionicPlatform.ready的特定条件下发送通知。对于条件,我使用$http从webservice获取数据,但不幸的是,我无法从$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); 
})  

我无法从功能$http.get函数中获取数据。如果我试图警告它出警报功能undefined

请帮助如何获取数据。

+0

这已被问及(并回答)至少在单独的Stackoverflow上千次。 – jcaron

+0

[我如何从异步调用返回响应?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jcaron

回答

0

这是因为$ http是一个异步调用,然后当来自http调用的响应可用时调用(), 和您的alert()在来自网络调用的响应可用之前同步执行。

读在这里详细 Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

$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); 

    }); 
}) 

现在您的警报运行时,你从$ HTTP调用的响应。

希望这可以帮助...

+0

谢谢@ mahiraj2709的回复...我试过异步代码引用,但没有运气,我只是想'$ http.get'函数中的值 – Nupur

+0

为什么你不把你的警报放在由promise (函数(响应) {var test = response.data [0]; if(test.result = ='test') { conditionvariable =“false”; } alert(conditionvariable); }); – mahiraj2709

+0

我必须承诺超出承诺的价值,这就是为什么我提醒外面 – Nupur