Reputation: 3317
I was looking on the internet but did not see a definitive answer. So here is my case:
The code is written by head so excuse for not being complete I have a page that looks something like:
<div ng-controller="myController">
<my-customer my-callback="directiveCallback(item)" />
</div>
I then have a script that looks like this:
var app = angular.module('myApp', []);
app.controller('myController', ['$scope', 'apiService', function($scope, apiService) {
apiResponse = apiService.getAll('myRepo');
apiResponse.then(function(d){
$scope.data = d;
})
$scope.directiveCallback = function(item) {
// do stuff with $scope.data
}
}])
.directive('myCustomer', function() {
return {
scope: {
myCallback:'&'
},
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
The thing is the service promise resolves and returns some data. The directive controller is calling another service that returns some data and fires the call back when it is done with the data that it recives.
When he lands on the callback function the item is attached to the $scope.data.
The problem was that the callback is triggered before $scope.data is populated. I curently salved this so that I wraped the callback in a $timeout with a settable delay via a direcitve parameter.
Is that is correct way to do it or is there a better way?
Upvotes: 0
Views: 20
Reputation: 9476
Hmm, doesnt this simply work:
$scope.directiveCallback = function(item) {
apiResponse.then(function() {
console.log($scope.data); // always updated
// do whatever
})
}
Upvotes: 1