I am developing a page with Angular, and have an init() method in my controller. The code is as follows:
var filtersController = ['$scope', '$http', function ($scope, $http) {
$scope.init = function () {
$http({
method: 'GET',
url: '/json-tags-test',
cache: true
}).success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
}];
It is just a call to a simple JSON file.
My HTML is as follows:
<div class="container main-frame" ng-app="projectsApp" ng-controller="filtersController" ng-init="init()">
</div>
For some reason, this get call gets call twice every time I load the page. Is this standard behaviour?
Many thanks,
Dash
I don't think it is getting called twice, i just created a plunk for you to see this.
var app = angular.module('projectsApp', []);
app.controller('filtersController', ['$scope', '$http', function ($scope, $http) {
$scope.status = 'Page loading';
var count = 0;
$scope.init = function () {
$scope.status = 'API called';
$http({
method: 'GET',
url: '/json-tags-test',
cache: true
}).success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
console.log('success');
count++;
$scope.status = 'successful: '+ count;
}).error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('error');
count++;
$scope.status = 'failed times: '+ count;
});
};
}]);
XHR logs from DEV tools
Edit:
Updated plunk with a dummy json file
http://plnkr.co/edit/hTdtLK?p=preview
As you can see once again that its getting called only once. Clear the logs i guess you are seeing the logs for the previous page load, coz changes are immediately visible when in preview mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With