Skip to content

Commit f270b2a

Browse files
Added another loading indicator example, w/o the use of httpInterceptor
1 parent d14fbe8 commit f270b2a

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
</head>
6+
<body>
7+
8+
<div ng-app="loadingIndicatorApp" ng-controller="appController">
9+
10+
<div loading-indicator></div>
11+
12+
<div ng-view>
13+
14+
</div>
15+
16+
</div>
17+
18+
<script src="../bower_components/angular/angular.min.js"></script>
19+
<script src="../bower_components/angular-route/angular-route.min.js"></script>
20+
21+
<script>
22+
23+
var app = angular.module("loadingIndicatorApp", ["ngRoute"]);
24+
25+
26+
app.config(['$routeProvider', function($routeProvider) {
27+
$routeProvider.
28+
when('/', {
29+
template: "<h2>Item List</h2><div ng-repeat=\"item in items\"><div><a href=\"#/items/{{item.id}}\">{{item.id}} - {{item.description}}</a></div></div></div>",
30+
controller: "itemListController",
31+
resolve : {
32+
data : itemListController.loadItems
33+
}
34+
}).
35+
when('/items/:itemId', {
36+
template: "<h2>Item Detail</h2><div>ID: {{item.id}}, Description: {{item.description}}</div>",
37+
controller: "itemController",
38+
resolve : {
39+
data : itemController.loadItem
40+
}}).
41+
otherwise({redirectTo: '/'});
42+
}]);
43+
44+
45+
app.directive("loadingIndicator", function() {
46+
return {
47+
restrict : "A",
48+
template: "<div>Loading...</div>",
49+
link : function(scope, element, attrs) {
50+
scope.$on("loading-started", function(e) {
51+
console.log("loading started")
52+
element.css({"display" : ""});
53+
});
54+
55+
scope.$on("loading-complete", function(e) {
56+
console.log("loading completed");
57+
element.css({"display" : "none"});
58+
});
59+
60+
}
61+
};
62+
});
63+
64+
app.controller("appController", function($scope, $rootScope) {
65+
66+
$rootScope.$on("$routeChangeStart", function(e) {
67+
//show indicator
68+
$rootScope.$broadcast("loading-started");
69+
});
70+
$rootScope.$on("$routeChangeSuccess", function(e) {
71+
//hide indicator
72+
$rootScope.$broadcast("loading-complete");
73+
});
74+
$rootScope.$on("$routeChangeError", function(e) {
75+
//show error..
76+
});
77+
78+
});
79+
80+
var itemController = app.controller("itemController", ['$scope', 'data', function($scope, data) {
81+
$scope.item = data;
82+
83+
}]);
84+
85+
itemController.loadItem = ['$q', '$http', '$route', function($q, $http, $route) {
86+
var config = {
87+
params: {
88+
'rows' : 1,
89+
'id' : $route.current.params.itemId,
90+
'description': '{lorem|10}',
91+
'delay' : 1,
92+
'callback': "JSON_CALLBACK"
93+
}
94+
};
95+
96+
var deferred = $q.defer();
97+
98+
99+
$http.jsonp("http://www.filltext.com", config, {})
100+
.success(function(data) {
101+
deferred.resolve(data[0]);
102+
})
103+
.error(function() {
104+
deferred.reject("Failed to fetch item.")
105+
});
106+
107+
return deferred.promise;
108+
109+
110+
}];
111+
112+
var itemListController = app.controller("itemListController", ['$scope', 'data', function($scope, data) {
113+
114+
$scope.items = data;
115+
116+
}]);
117+
118+
itemListController.loadItems = ['$q', '$http', function($q, $http) {
119+
var config = {
120+
params: {
121+
'rows': 10,
122+
'id' : '{index}',
123+
'description': '{lorem|10}',
124+
'delay' : 1,
125+
'callback': "JSON_CALLBACK"
126+
}
127+
};
128+
129+
var deferred = $q.defer();
130+
131+
$http.jsonp("http://www.filltext.com", config, {})
132+
.success(function(data) {
133+
deferred.resolve(data);
134+
})
135+
.error(function() {
136+
deferred.reject("Failed to fetch items.")
137+
});
138+
139+
return deferred.promise;
140+
141+
}];
142+
143+
144+
</script>
145+
146+
</body>
147+
</html>

0 commit comments

Comments
 (0)