1. 在項目中,會遇到初始化頁面是一個列表,但頁面中包含有一個input搜索框,輸入關鍵字搜索的時候,列表需要實時變化,列表條目總數也需要實時變化。類似下圖的需求。
解決方案如下:
<input placeholder="輸入鏡像名稱查找" ng-model="projectImageKey"/>
<ul>
<li ng-repeat="item in projectImagesFiltered=(imageProjectCollections|filter:{'projectCollectionName ':projectImageKey})|orderBy:'projectCollectionName '"></li>
</ul>
說明:
projectImageKey:表示輸入的搜索關鍵字
item : 表示列表數據裏面的每一條目
imageProjectCollections:拿到的最原始的數據列表
projectCollectionName : 列表進行排序的關鍵字段
projectImagesFiltered :經過排序(orderby),經過過濾(filter)之後的數據
li的這句話要做的事就是:①原始數據列表imageProjectCollections按照列表每一條item 裏的屬性projectCollectionName 排序 ②再對上一步得到的數據列表進行過濾(過濾條件是projectCollectionName 屬性與搜索關鍵字projectImageKey相同的數據)得到一個新的列表 ③將經過一系列處理的數據列表賦值給一個變量projectImagesFiltered,這樣就可以做到搜索實時變化列表,用projectImagesFiltered獲取到的length也是實時變化的