This is my usual approach. To OZ_'s point, this requires Font Awesome. The <i>
's classes fa fa-spinner fa-spin
are a reference to that library.
Although, you could also opt to show/hide a .gif
that indicates loading.
Markup
Using ng-hide
and ng-show
to control visibility of the spinner and element that will contain your populated data.
<p class="text-center" ng-hide="dataLoaded">
<i class="fa fa-spinner fa-spin"></i>
</p>
<div ng-show="dataLoaded">
<a>Countries</a> : {{Countries.length}}
</div>
JS
Before your call, set $scope.dataLoaded
to false
. Then, within your success
block, set it to true
. Also worth noting you'll need to pass $scope
to your factory.
app.factory('countriesService', function($http, $scope) {
return {
getCountryData: function(done) {
$scope.dataLoaded = false;
$http.get('/resources/json/countries.json')
.success(function(data) {
done(data);
$scope.dataLoaded = true;
})
.error(function(error) {
alert('An error occured');
});
}
}
});