$ http获取逻辑只允许200 OK

时间:2016-02-05 13:14:06

标签: javascript angularjs http ionic-framework

我试图在我的api中构建一些逻辑,因此它将排除在字段中具有URL的对象" image"返回4xx,5xx或超过4000ms的时间。

下面的代码并不是很有用 - 因为它仍然允许404和空白图像。

有人可以建议如何解决这个问题吗?

此外,这里是一个plunker- http://plnkr.co/edit/7ScnGyy2eAmGwcJ7XZ2Z?p=preview

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);
        });

      })

2 个答案:

答案 0 :(得分:0)

我改变了这一行:

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

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

它运作正常。

答案 1 :(得分:0)

<强> The plunker

我将ng-if="card.image"添加到<td-card ng-if="card.image" ng-repeat="card in cards" ...

如果没有可用的图像,这将阻止模板生成并清空图像模板