我希望能夠在過濾器後引用ng-options
中的項目數。這可能嗎?如何在ng選項中引用已過濾的數組
例如,此工作原理:
<select ng-model="selectedItem"
ng-options="_item.id as _item.name for _item in PageModel.objects"
ng-show="PageModel.objects.length>0">
<option value="">-- Select --</option>
</select>
<span ng-show="PageModel.objects.length==0">No objects to display</span>
我可以引用陣列PageModel.objects
的長度,並使用該轉動部件打開或關閉。 但我該如何做相同的過濾數組?
<select ng-model="selectedItem.objectId"
ng-options="_item.id as _item.name for _item in PageModel.objects | filter:{type:filterBy}"
ng-show="PageModel.objects.length>0">
<option value="">-- Select --</option>
</select>
<span ng-show="PageModel.objects.length==0" >No objects to display</span>
這顯然總是會返回原始數組的長度,但是有沒有一種方法來引用過濾列表的變化?
這是我怎麼會想到它的工作:
ng-options="_item.id as _item.name for _item in PageModel.objects | filter:{type:filterBy} as filteredObjects"
然後才能夠使用這個變量filteredObjects
這樣的:
<span ng-show="filteredObjects.length==0" >No objects to display</span>
但是,這是行不通的。
這是一個小提琴,展示了這個例子的工作。我希望值(8)是動態的。
fiddle example (按鈕只是用來模擬所期望的結果)
我知道我可以使用模型上的手錶做,但可以用另一種方式來完成類似於上面?
感謝額外的信息。你和PinBack在同一時間得到了同樣的答案。 – DeclanMcD 2015-02-11 11:56:26
不客氣,謝謝選擇我的最佳答案! – 2015-02-12 11:49:22