Angularjs服务函数在Rest API响应之前返回响应。
app.factory('LoginService', function ($http, $state) {
isAuthenticated = false;
return {
login: function (userName, password) {
$http.get(gadgetConfigurations.server_url_local + apis.authenticate +
'/' + userName + '/' + password)
.then(function (response) {
console.log('Response from Login api: ', response.data.length);
if (response.data.length > 0) {
isAuthenticated = true;
console.log('Response rec from rest api', isAuthenticated);
}
},
function (error) {
console.log('Login api error: ' + error);
})
console.log('Response sent', isAuthenticated);
return isAuthenticated;
},
isAuthenticated: function () {
return isAuthenticated;
}
};
});
</code></pre>
// Response sent "false"
// Response rec from rest api "true"