javascript - 如果这些属性中的任何一个为空,则不显示该项目

标签 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'));
    }
  })

如您所见,我需要如果属性“rating”或“poster”出现在 null 中,则不要在 View 中显示该元素,这是正确的情况,因为你看,rating 出现在 null 中,所以我需要隐藏整个元素。

查看:

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

这将为您提供一个没有没有评级/海报的节目的数组。

关于javascript - 如果这些属性中的任何一个为空,则不显示该项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358682/

相关文章:

javascript - 有没有办法处理GOOGLE APPS SCRIPT库的未定义方法?

javascript - 如何围绕他的中心旋转元素?

javascript - 用一点逻辑删除元素

javascript - Angular js使用checked()而不是indexof()作为复选框

javascript - Angular.js 中的逆波兰表示法计算器

javascript - 网站静态 MP3 播放器

javascript - 等待图像加载到 angularjs 工厂内

javascript - Controller 中的 $scope 在 View 中不可访问/数据绑定(bind)不起作用(IONIC/ANGULAR)

javascript - Angular 指令模板输入不会更新指令范围

javascript - 将主干模型绑定(bind)到 Marionette ItemView - 阻塞 .fetch()?