Reputation: 2436
I have the following:
app.controller('MyController', function($scope, MyAdapter) {
MyAdapter.getContactInfo(123, function(response) {
$scope.contactInfo = JSON.parse(response.result);
});
}
app.service("MyAdapter", function() {
this.getContactInfo = function(id, callback) {
thirdPartyAPI(id, callback);
};
});
Essentially my controller, calls a service to invoke an asynchronous function in a 3rd party api. I need to update the scope in controller with the response but it seems I don't have access to the scope from the anonymous function. Is there a way around this?
Upvotes: 1
Views: 2299
Reputation: 37530
You should have access to the scope in the callback. What is probably happening is that because it's a 3rd party async call updating the scope, angular doesn't know about it. You need to use $scope.$apply()
to trigger a digest cycle.
app.controller('MyController', function($scope, MyAdapter) {
MyAdapter.getContactInfo(123, function(response) {
$scope.contactInfo = JSON.parse(response.result);
$scope.$apply();
});
}
Upvotes: 3