I have this code snippet which is causing an issue:
$http.get('/api/users').
success(function(data) {
$scope.authors = data;
}).
error(function() {
console.log('API error - configuration.')
});
Further down in the code:
for (var i = 0; i < $scope.authors.length; i++) {
...
};
Sometimes, the $scope.authors
object is not yet available. Is there a way to handle this situation?
UPDATE
Here is the complete block structure of the code:
// author
$http.get('/api/users').
success(function(data) {
$scope.authors = data;
processAuthors();
}).
error(function() {
console.log('API error - configuration.')
});
// handling form updates
$scope.$on('$routeChangeSuccess', function() {
if($routeParams.id) {
$http.get('/api/offers/' + $routeParams.id).
success(function(data) {
// author
function processAuthors() {
for (var i = 0; i < $scope.authors.length; i++) {
if($scope.authors[i].email == data.author.email) {
$scope.author = $scope.authors[i];
};
};
}