Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Quick Example of the Ionic Loading Widget

DZone's Guide to

A Quick Example of the Ionic Loading Widget

Learn how to easily integrate a loading widget into your app.

· Mobile Zone ·
Free Resource

One of the things I love most about Ionic is how rapidly you can build applications. Many of the cooler features are simple things that can be quickly implemented for an easy win. I like easy wins. Here is a great example of that – the Ionic Loading widget.

Imagine you’ve got a simple service method runs over HTTP. This process can be fast or slow based on network conditions, size of the data, and other factors. (Like the Force. Hey, it can happen.) Your code probably looks like this:

.controller('SearchCtrl', function($scope,DataService) {
$scope.search = {property:''};
$scope.results = [];

$scope.doSearch = function() {
if($scope.search.property === '') return;
$scope.results = [];
DataService.searchProperties($scope.search.property).then(function(res) {
$scope.results = res;
});
}

})

We’re not concerned about the service itself. It returns a promise and will take “some time” to return. So if that service happens to be slow today, it could look like this:


Notice on click there is no visual feedback to the user that anything is happening. If they are impatient (and what user isn’t), they could click multiple times and fire off numerous Ajax requests. Let’s fix that:

.controller('SearchCtrl', function($scope,DataService,$ionicLoading) {
$scope.search = {property:''};
$scope.results = [];

$scope.doSearch = function() {
if($scope.search.property === '') return;
$scope.results = [];
$ionicLoading.show();
DataService.searchProperties($scope.search.property).then(function(res) {
$scope.results = res;
$ionicLoading.hide();
});
}

})

There are precisely three changes here. I added $ionicLoading to the controller – I ran the show() method on it before I began the async process – and finally I hid it using hide(). That’s it. I could customize the widget with a message if I was feeling fancy, but today isn’t a fancy day. Here is the change:

Ok, so this isn’t exactly rocket science, but for about 30 seconds of coding I got a much improved experience.


Topics:
ionic

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}