我正在使用 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/