2016-12-05 105 views
0

更新的问题和解决方案的提供。 我从https://filters.rocks/embed/466.json得到一个JSON饲料。NG重复

的JSON是未来通过我的范围,但我可以在我的前标签,但不管是什么原因,它只是不填充我的NG-重复观看。

app.controller('InstagramCtrl', ['$scope', '$http', 
function($scope, $http) { 
    $scope.items = []; 
    $http.get('https://filters.rocks/embed/466.json').then(function(response) { 
      $scope.items = response; 
      instagramSuccess($scope, $scope.items); 

    }); 

    var instagramSuccess = function(scope, response) { 
     if (response.data.length <= 0) { 
      $scope.error = response.meta.error_type + ' | ' + response.meta.error_message; 
      return; 
     } 
     if (response.data.length > 0) { 
      $scope.items = response.data; 

     } else { 
      $scope.error = "This hashtag has returned no results"; 
     } 
    }; 

} 
]); 

HTML

<div class="container" ng-controller="InstagramCtrl"> 

    <div class="row"> 
     <div class="col-md-12"> 
      <div class="panel panel-default"> 
       <div class="panel-heading"> 

       </div> 
       <div class="panel-body"> 
        <ul class="instagram-list" ng-repeat="item in items">  
         <li> 
          <small>{{item.data.feed.id}}</small> 
          <img ng-src="{{item.data.feed.approved_images.medium}}" width="250"> 
         </li> 

        </ul>      
       </div> 
       <pre> filter.rocks {{ items | json }} </pre> 
      </div> 
     </div>   
    </div> 

更新 我把点点滴滴,从每个人,结束了这一点; 控制器

$scope.items = []; 

     $http.get('https://filters.rocks/embed/466.json').then(function(response) { 

      instagramSuccess(response); 

    }); 

    var instagramSuccess = function(response) { 
     if (!response.data) { 
      $scope.error = response.meta.error_type + ' | ' + response.meta.error_message; 
      return; 
     } 
     // check if data is defined if so, check objects are defined, then if array is defined. 
     if (response.data && response.data.feed && response.data.feed.approved_images && response.data.feed.approved_images.length) { 
      //if all good $scope.items becomes array 
      $scope.items = response.data.feed.approved_images; 

     } else { 
      $scope.error = "This hashtag has returned no results"; 
     } 
    }; 

HTML

    <ul class="instagram-list" ng-repeat="item in items">  
         <li class="instagram-item"> 
          <small style="color:blue;">{{item.id}}</small> 
          <div bg-img="{{item.medium}}" width="250"></div> 
         </li> 
        </ul> 
+1

打印{{items}}并查看您是否正在调用正确的子属性。这是一个常见的错误,你会收到一个地图,但你继续迭代它作为一个列表。 – madhairsilence

回答

2
作为

指示madhairsilence,你是治疗项,就好像它是一个数组时,它不是。你可能会翻翻approved_images重复,如:

<ul class="instagram-list" ng-repeat="item in items.feed.approved_images">  
    <li> 
     <small>{{item.id}}</small> 
     <img ng-src="{{item.medium}}" width="250"> 
    </li> 
</ul> 
+0

完全忘记了对象返回的长度为0或null,因此为什么没有通过。我用他的解决方案更新了我的问题,并使用您的逻辑并进行了一些修改。 – Brendan

1

与以下内容......没有必要在你的controller..you任何改变的循环在错误的项目更改HTML

<div class="panel-body"> 
      <ul class="instagram-list" ng-repeat="item in items.data.feed.approved_images">  
       <li> 
        <small>{{item.id}}</small> 
        <img ng-src="{{item.medium}}" width="250"> 
       </li> 

     </ul>      
    </div> 
+0

我扩展了你的想法,并加入到我的错误检查中。 – Brendan

+0

是的..检查长度 –