this is a follow up to my previous question.
I have an AJAX request which takes some time. Let's say I get two parts of data, var1 and var2. I only want to use var1 in a directive that is bound to main controller scope with
var1: '=',
I get var1 and var2 combined from the service but since they have a different purpose, I cannot simply use the one promise that I get from $http call.
I tried to implement it but it still not working as I supposed. The service is of following form:
myModule.service('MyService', ['$http', '$q', function($http, $q) {
var var1 = $q.defer();
var var2 = $q.defer();
httpPromise = $http({
url: "/myService",
method: "GET",
params: {}
}).success(function(data, status) {
var1.resolve(data.var1);
var2.resolve(data.var2);
})
return {
getVar1: function() {
return var1.promise;
},
getVar2: function() {
return var2.promise;
},
}
}]);
In the controller I assign the promises as follows:
$scope.var1 = MyService.getVar1();
$scope.var2 = MyService.getVar2();
However once the promise is resolved in the service, the directive seems not to know that value for var1 was changed. I thought that this will be handled by AngularJS engine.
It seems to me that the only way here is to make a lot of then() calls that would introduce more mess than good. I think I will stay with (an ugly hack I suppose...)
$scope.$watch('var1', ...)
inside the directive, unless someone shows me a better direction.