我正在使用Angularjs,并且收到了这样的对象数组:

{
  "_id": 310243,
  "name": "Five Star Babies: Inside the Portland Hospital",
  "airsDayOfWeek": "Wednesday",
  "airsTime": "21:00",
  "firstAired": "2016-04-13T00:00:00.000Z",
  "network": "BBC Two",
  "overview": "An exclusive glimpse inside the UK's only private maternity hospital.\r\n",
  "rating": null,
  "ratingCount": 0,
  "status": "Continuing",
  "poster": "/someImage/path",
  "subscribers": []
}


实际上,该对象来自此函数:

  $scope.shows = Show.query(); // returns an array
  _.forEach($scope.shows, function(item) {
    if (item.rating === null) {
      console.log(angular.toJson(item, 'pretty'));
    }
  })


如您所见,我需要如果null中包含“ rating”或“ poster”属性,则不要在视图中显示该元素,这是合适的情况,如您所见,rating来自因此,我需要隐藏整个元素。

视图:

    <div ng-repeat="show in shows | filter:query | orderBy:'rating':true">
      <a href="/shows/{{show._id}}">
        <img ng-src="{{show.poster}}" width="100%"/>
      </a>
      <div>
        <a href="/shows/{{show._id}}">{{show.name}}</a>
        <p>
          Episodes: {{show.episodes.length}} <br>
          <span>Rating: {{show.rating}}</span>
        </p>
      </div>
    </div>


有什么建议么?

最佳答案

似乎您可以只过滤所需值的数组。

$scope.shows = Show.query().filter(function(val) {
    if (val.rating !== null && val.poster !== null) return val;
});


这将为您提供没有评级/海报的节目,而没有这些节目。

10-06 09:08