2015-02-11 37 views
0

我希望能夠在過濾器後引用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 (按鈕只是用來模擬所期望的結果)

我知道我可以使用模型上的手錶做,但可以用另一種方式來完成類似於上面?

回答

1

您應該應用在NG-顯示相同的過濾器了。

<select ng-model="selectedItem.objectId" 
ng-options="_item.id as _item.name for _item in PageModel.objects | filter:{type:filterBy}" 
ng-show="(PageModel.objects|filter:{type:filterBy}) && (PageModel.objects|filter:{type:filterBy}.length>0"> 
<option value="">-- Select --</option></select> 

爲了更好地解釋你可以看看這個鏈接:http://php.quicoto.com/use-ng-show-filtering-data-angularjs/

再見

+0

感謝額外的信息。你和PinBack在同一時間得到了同樣的答案。 – DeclanMcD 2015-02-11 11:56:26

+0

不客氣,謝謝選擇我的最佳答案! – 2015-02-12 11:49:22

1

試試這個:

Select from a list of {{(PageModel.objects|filter:{type:filterBy}).length}}: <-- need to be able to reference/use this? 
相關問題