This is a follow up question for Angularjs $http wait for response
Since i was unable to find a solution for that, i thought i will return a promise always and let my directive do the work in promise.then() function.
$scope.getVCard = function(id){
var vcardKey = vcardKeyPrefix+id;
var vCardFromLS = localStorageService.get(vCardKey);
if(vCardFromLS){
var deferred = $q.defer();
deferred.resolve({data:localStorageService.get(vCardKey)});
return deferred.promise;
}
}
and in my directive i am using it as
(function(angular, app) {
app.directive('popOver',["$window","$http",function($window,$http){
return function(scope,elem,attrs){
elem.on('mouseover',function(){
console.log('mouseover');
var promise = scope.$apply(attrs.popOver);
promise.then(function(data){
console.log('promise then called');
console.log(data);
//logic here
});
console.log('in directive again');
console.log(data);
});
};
}]);
})(angular, app);
But promise.then() is not getting invoked on first time. It gets invoked and works fine on subsequent mouse overs. What can be the issue?
I tried adding $scope.$apply() just before return deferred.promise but i am getting apply already in progress error. What am i missing here?
vCardFromLSis found. Are you sure it always exists?scope.$applyconfused Angular. Here is a slighty rewritten version that seems to work okay.