$ http Get logic to allow 200 OK

I am trying to create some logic in my api to exclude objects that have a URL in the "image" field, which returns 4xx, 5xx or times out for more than 4000 ms.

The code below does not work, as it still permits 404 and blank images.

Can someone tell me how to fix this?

In addition, here he is in plunker- http://plnkr.co/edit/7ScnGyy2eAmGwcJ7XZ2Z?p=preview

Example json: {id: 242, image: "www.image.com/1.jpg", name: "blah blah"}

.controller('CardsCtrl', ['$scope', '$http', '$state', '$q', '$ionicLoading', '$ionicModal', 'TDCardDelegate', 'cardsApi', '$http', '$timeout', '$element', function($scope, $http, $state, $q, $ionicLoading, $ionicModal, TDCardDelegate, cardsApi, $http, $timeout, $element) { var loginuid = window.localStorage.getItem('uid'); console.log(loginuid); var MIN_CARDS = 7; console.log('CARDS CTRL'); $scope.cards = []; cardsApi.getApiData() .then(function(result) { console.log(result.data); //Shows log of API incoming $scope.cards = result.data; result.data.forEach(function(card) { var imgurl = card.image; console.log(imgurl); var canceler = $q.defer(); $http.get(imgurl, { timeout: canceler.promise }) .then(function mySuccess(response) { console.log(response.status); }, function myError(response) { console.log(response.status); if (response.status == 404) { $scope.cards.splice(card, 1); } // console.log('deleted'); var imgpid = "#card-image-" + card.vari; console.log(imgpid); $(imgpid).parent().remove(); }) .catch(function(err) { }); $timeout(function() { $ionicLoading.hide(); canceler.resolve(); }, 4000); }); }) 
+5
source share
2 answers

I changed this line:

 <img class="storelogo-image" ng-src="{{card.store__logo}}"> 

to

  <img class="storelogo-image" ng-src="{{card.store__logo.indexOf('http://')> -1 ?card.store__logo :''}}"> 

And it works great.

0
source

Plunker

I added ng-if="card.image" to <td-card ng-if="card.image" ng-repeat="card in cards" ...

This will prevent the creation of a template and an empty image template in the absence of an image.

0
source

Source: https://habr.com/ru/post/1242372/


All Articles