如果其中任何属性为空,则不显示该项

时间:2016-05-21 03:51:08

标签: javascript angularjs

我正在使用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'));
    }
  })

如你所见,我需要的是如果财产&#34;评级&#34;或者&#34;海报&#34;来自null,然后不显示视图中的元素,这是正确的情况,如您所见,评级来自null所以我需要隐藏整个元素。< / p>

查看:

    <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>

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

好像你可以只为你想要的值过滤你的数组。

DataContextChanged

那将给你一个没有评级/海报的节目。

答案 1 :(得分:0)

试试这个:

<div ng-repeat="show in shows | filter:query | orderBy:'rating':true" ng-hide="show.rating == null || show.poster == null">
      <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>

使用ng-repeat

添加ng-hide="show.rating == null || show.poster == null"

您也可以查看这个问题:How to show/hide if variable is null