Attempting to assign data to the scope after an asynchronous call.
There is an array named companies in the factory.
factory.getByCategoryId = function (id) {
$http.get('http://localhost/campaign?access-token=12345&id=2').then(
function (result) {
factory.companies = [];
if (result.data.length !== 0) {
for (var index = 0; index < result.data.length; index++) {
factory.companies.push(result.data[index]);
}
} else {
console.log('Result is not set.', result);
}
},
function (result) {
console.log('Failed', result);
}
);
}
Function invocation:
$scope.closeModal = function (index) {
if (index) {
var promises = []
promises.push(Service.getByCategoryId($scope.categories[index].id));
$q.all(promises).then(function () {
$scope.myItems = Service.all(); //return all from factory.companies
$scope.refreshItems(); //this function refreshes the DOM using myItems
});
}
}
The first time this function is called, factory.companies remains unchanged, but it gets updated on subsequent calls. There seems to be a delay before the update takes place, but I'm uncertain how to address it.