I have this directive :
app.directive('changemonth', function($animator) {
return {
link : function($scope, element, attrs) {
element.bind("click", function() {
if(element.attr('class').search('disabled') == -1) {
// récupération du calendrier :
if(element.attr('class').search('day') != -1)
var calendar = angular.element(element.parent().parent());
else
var calendar = angular.element(element.parent().parent().next().children()[1]);
var animator = $animator($scope, attrs);
animator.hide(calendar);
setTimeout(function() {
$scope.$apply(attrs.changemonth);
animator.show(calendar);
}, 500);
}
});
}
};
});
With attrs.changemonth, i call a function (wich can change), for exemple this one :
$scope.next = function() {
var tmpMonth = $scope.monthsLabels.indexOf($scope.monthDisplayed) + 1;
var tmpYear = $scope.yearDisplayed;
if(tmpMonth==12) {
tmpMonth = 0;
tmpYear = parseInt($scope.yearDisplayed) + 1;
}
getCalendar(tmpMonth, tmpYear);
$scope.monthDisplayed = $scope.monthsLabels[tmpMonth];
$scope.yearDisplayed = tmpYear.toString();
};
And so this function call another getCalendar() you can see here :
function getCalendar(month, year) {
$http({
method : "GET",
url : 'http://my_url/getCalendar',
params : {
group_id : $scope.group_id,
month : month,
year : year
}
})
.success(function(data) {
$scope.calendar = data;
});
}
getCalendar() use $http to get the calendar from database.
My problem is that i would like to wait for the response of $http before using animator in directive, like this, my calendar will only be displayed if the content is loaded.
I heard of $q and promises. But I don't see how to use it in this very particular context.
It would be awesome if somebody here have an idea.