[英]Angularjs - How do we watch dynamic elements from ngRepeat, with dynamic ng-model names, and filter element values on ANY change?
[英]How to use ngRepeat on element returned from a function (a filter, actually) - AngularJS?
我需要使用以下內容:
<ul>
<li ng-repeat='item in getShopItems(shop.id)'>{{item.name}}</li>
</ul>
我必須使用一個函數,因為我需要ngRepeat
的當前元素來應用過濾器。
getShopItems(id)
函數如下:
$scope.getShopItems = function(id){
filteredItems = $filter('filter')($scope.items, {shop_id: shop_id});
return filteredItems;
}
使用ngInit
可以提供幫助,但官方文檔不鼓勵它。
如果我們不能使用ngRepeat
函數,我應該如何應用過濾器。 提前致謝。
這里有一個吸血鬼: http ://plnkr.co/ugOZ7QgQ3EHVQUGWTCII [不工作]
編輯: provider
和shop
是相同的
編輯:我不想在視圖中過濾item in items | filter:{}
item in items | filter:{}
您可以將過濾器用作自定義過濾器:
<ul>
<li ng-repeat='item in items | filter:getShopItems'>{{item.name}}</li>
</ul>
並稍微修改過濾功能:
$scope.getShopItems = function(item) {
return item.provider_id == $scope.shop.id;
}
我想,你有兩個解決方案,要么在控制器中初始化你的陣列:
function getShopItems(id){
filteredItems = $filter('filter')($scope.items, {provider_id: provider_id});
return filteredItems;
}
$scope.filterItems = getShopItems($scope.shop.id)
對於意見:
但是,這種解決方案可能不適用於您的情況,例如,如果您的ID可以改變。
另一種方法是直接在視圖中使用您的過濾器:
<ul>
<li ng-repeat='item in items|filter:{"id":shop.id}'>{{item.name}}</li>
</ul>
我還建議你閱讀這個帖子: 如何循環使用ng-repeat函數返回的項目?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.