SerzN1 SerzN1 - 6 months ago 8
AngularJS Question

How to organize the download of data with the authorization check in Angular?

When you step on the rout triggered a particular controller and loads the data.

angular.module('app.Images', [])

.controller('ImagesCtrl', function($scope, $http, $window, imagesUrl) {
var _this = this;

this.images = {};

$http.get(imagesUrl)
.then(checkResponse);

function checkResponse (data) {
if (data.success && data.data) {
_this.images = data.data;
} else if (data.error) {
alert(data.error);
} else if (data.reload) {
$window.location.reload(true);
}
}


On each route have to check
data.reload
.

How best to do data load and such checks at all entry points?

Thanks for the answers.

Answer

If You need to globally check the data.reload (if i understand You correctly) then You can user interceptors ($httpProvider.interceptors). You can read about it here in the interceptors part.

$httpProvider.interceptors.push(['$q', function ($q) {
  return {
    'response': function(result) {
      //check data.reload here
      return $q.resolve(result);
    }
  };
}]);