Search code examples
angularjsget

AngularJS HTTP.get not fired/displayed in network tab


When firing a http GET request with AngularJS multiple times the .then callback is called and there is a response but there is no request displayed on the network tab of my browser and the content of the response is wrong (content of the previously fired request). I assume it's some AngularJS/browser caching problem since when its cached on the backend the request would be displayed. I tried several things like disabling the cache but nothing helps. Could it has something to do that the promise of the http request is returned from a factory?

angular.module('eventLogServiceApp', [])
    .factory('eventlog.service', ['$http', createEventLogService]);

function createEventLogService($http) {
  return {
      getNotable: $http.get('https://myurl.com/api/endpoint')
  }
}

angular.module('myApp')

.controller("AdminEventLogController", ['$scope', 'TranslationService', 'eventlog.service', function ($scope, trans, EventLog) {
    $scope.getNotable = function() {
        EventLog.getNotable.then(function success(response) {
            $scope.notable = response.data.length;
        }, function error() {
        });
    };
 }])

I found out if I wrap the promise in a function the request is displayed in the network tab. I don't know why this works but it does. Example shown below:

angular.module('eventLogServiceApp', [])
    .factory('eventlog.service', ['$http', createEventLogService]);

function createEventLogService($http) {
  return {
      getNotable: function(){
         return  $http.get('https://myurl.com/api/endpoint')
      }
  }
}

angular.module('myApp')

.controller("AdminEventLogController", ['$scope', 'TranslationService', 'eventlog.service', function ($scope, trans, EventLog) {
    $scope.getNotable = function() {
        EventLog.getNotable().then(function success(response) {
            $scope.notable = response.data.length;
        }, function error() {
        });
    };
 }])

EDIT: Added controller/factory


Solution

  • You should wrap your logic into a function and return it there, otherwise it will be loaded only once on initial request.