This is a little complicated. I will try my best to explain my question.
First, I create a HttpService
to wrap $http
so that every ajax will trigger the #loading
element to show that a request is processing. This is the service below.
angular.module('HttpServices', [])
.factory('HttpWrapper', ['$http', '$rootScope', function($http, $rootScope) {
$http.defaults.transformRequest.push(function(data) {
$rootScope.loading = ($rootScope.loading ? $rootScope.loading + 1 : 1);
console.log('start request') # this will be triggered by template load!!
console.log(data); # the template will be printed to the console!!
return data;
});
$http.defaults.transformResponse.push(function(data) {
$rootScope.loading -= 1;
console.log('finish request');
console.log(data);
return data;
});
return $http;
}]);
Then, I have a quit simple routes.
@myapp = angular.module('myapp', ['HttpServices'])
myapp.factory('Service', ['HttpWrapper', ($http) ->
service = {}
service.data = [1..3]
service
])
myapp.config (['$routeProvider', ($routeProvider) ->
$routeProvider
.when('/', {
templateUrl: 'home.html',
resolve: {
data: ['Service', (Service) ->
Service.data
]
}
})
.when('/test', {
templateUrl: 'test.html'
})
])
Then the html is quite simple as well.
<div data-ng-app='myapp'>
<ul>
<li><a href="#/">home</a></li>
<li><a href="#/test">test</a></li>
</ul>
<div id="loading" ng-show="loading">loading</div>
<div ng-view></div>
<script type="text/ng-template" id="home.html">
home
</script>
<script type="text/ng-template" id="test.html">
test
</script>
</div>
Look, the template is just inline template. They are just behide the ng-view
element. But when I click the link #/ or link #/test, the http wrapper will be triggered just like an ajax request is been send. Why? They are just inline template. Why trigger the $http
? Actually there is no communication with backend.