我正在使用Angular的前端,我想弄清楚如何做一個過濾器,以便在結尾處包含2個屬性的結果。可能是一個或其他財產。如何做一個過濾器爲了把結果包含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>
我在HTML做什麼,是由等級排序,你可以看到。問題是,如果rating
屬性爲null
,那麼該元素將位於結果的頂部,然後具有最高評分的元素就像我想要的那樣進入列表中。
這是我收到的對象:
{
"_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, // THIS PROPERTY COMES NULL
"ratingCount": 0,
"status": "Continuing",
"poster": "/someImage/path",
"subscribers": []
}
對象來自這裏:
$scope.shows = Show.query();
和$scope.shows
返回此:
[
{
"_id": 73787,
"name": "That '70s Show",
"airsDayOfWeek": "Thursday",
"airsTime": "8:00 PM",
"firstAired": "1998-08-23T00:00:00.000Z",
"network": "FOX (US)",
"overview": "Set in the Wisconsin suburbs...",
"rating": null, // PROPERTY NULL
"ratingCount": 87,
"status": "Ended"
"poster": "/some/Img/Path"
},
{
"_id": 35345,
"name": "Friends",
"airsDayOfWeek": "Thursday",
"airsTime": "8:00 PM",
"firstAired": "1998-08-23T00:00:00.000Z",
"network": "FOX (US)",
"overview": "New York TV Show...",
"rating": 9.1,
"ratingCount": 87,
"status": "Ended"
"poster": null // PROPERTY NULL
}
]
這就是對象的方式來,以及那些我需要在結果結束時放置的兩個屬性ey來吧null
,rating
或poster
。
TL; DR:
排序依據功能,其在端部使在所述結果與收視率最高的元素的頂部,並與rating
或poster
=== null
任何建議的元素?
調整數據或使用自定義過濾器 – charlietfl
@charlietfl如何調整數據是來自API? – TheUnnamed
可以在接收到數據時循環並將'null'更改爲'0',以便正確排序。並不鮮見需要操縱數據以更好地適合您的應用程序 – charlietfl