Below is my angular controller but in it, my promise doesn't seem to work. console.log isn't showing response nor is alert being shown over here.
myapp.controller('contrl6', ['$scope','$q','$localStorage','$http',
function ($scope,$q,$localStorage,$http) {
$scope.myXhr = function(){
var q = $q.defer();
$http({
method: 'GET',
url: '/api/session_data'
})
.success(function(response){
console.log(response);
q.resolve('request successful');
})
.error(function(response){
console.log("error");
q.reject('ERROR');
});
return q.promise;
}
console.log("hi");
var myPromise = $scope.myXhr();
myPromise.then(function(resolve){
alert(resolve);
console.log(resolve);
}, function(reject){
alert(reject);
console.log(reject);
});
}]);
console.log()calls showing output (even the 'hi')? If not then perhaps your code isn't wired up correctly and is never being called. Otherwise it looks as though the code should work (though it breaks a lot of the Angular conventions on how to do things).successanderrorbeing deprecated, you don't need$qhere as you can just use the promise from$http. The other conventions to follow would be giving your controller a name that ends withControllerand using$loginstead ofconsole.log(). None of which should affect whether the code works.