My HTML is
<div id="categories" ng-controller="AddTransactionCtrl">
<tr ng-repeat="category in categories">
<td><a href="{{category.name}}" target="_blank">{{category.name}}</a></td>
<td>{{category.description}}</td>
</tr>
</div>
I want to populate this list from Controller that fetches the data from server. My controller looks like
function AddTransactionCtrl($scope, $http) {
$scope.transaction = {};
$scope.transaction.categories = [];
$http({
method: 'GET',
url: '/categories'
}).success(function(data, status, headers, config) {
console.log('/categories succeeded. - ' + data.length);
$scope.transaction.categories = data;
}).error(function(data, status, headers, config) {
console.log('/categories failed.');
});
console.log($scope.transaction.categories.length);
$scope.save = function() {
console.log('will add transaction');
console.log('name:' + $scope.transaction.name);
console.log('date:' + $scope.transaction.date);
console.log('desc:' + $scope.transaction.desc);
}
}
When I refresh my page and see on Chrome console, I see
0
/categories succeeded. - 151
- Which means that
$scope.transaction.categoriesis not assigned - Also as I see logs
0is printed before the http log, which means the call is asynchronous and explains why the length is0
Question
a.) How can I make sure that the categories are populated by HTTP before anything related to rendering on page or console is printed out