2016-10-24 14 views
0

我在我的頁面上有一個ng-repeat,我需要過濾其中的項目。爲了過濾的目的,我在其上面包含了一個包含我需要使用的密鑰的隱藏字段。如何將包含ng-repeat語句的DOM對象(this)傳遞給ng-if函數? (Javascript/AngularJS)

<div class="filterProductsContainer"> 
    <asp:HiddenField runat="server" ID="FilterKey"/> 
    <ul class="product_listing_component--results-list"> 
     <li ng-if="isInCategory(product, $event)" ng-repeat="product in filterProducts"> 
      <a href="{{product.link}}"> 
       <img src="{{product.image}}" /></a> 
     </li> 
    </ul> 
</div> 

$scope.isInCategory = function (product, $event) { 
    console.log(product); 
    console.log($event); 
    var filterKey = $($event.target).parent(".filterProductsContainer").find("input[type='hidden'").val(); 
    console.log(filterKey); 
    var targetProduct = $scope.products[product]; 
    var foundMatch = false; 
    for (var tag in targetProduct.tags) { 
     var targetTag = targetProduct.tags[tag]; 
     if (filterKey === targetTag.id) { 
      foundMatch = true; 
     } 

     if (foundMatch) { 
      break; 
     } 

    } 
    return foundMatch; 
} 

$事件最終被空,但。

回答

0

嘗試直接在利用濾波器​​NG-重複

<li ng-repeat="product in filterProducts | filter: {id: '<FilterKeyValue>'}" > 

哪裏id是您要使用由過濾您product對象的屬性名稱。

+0

它將如何解釋FilterKeyValue?這是從哪裏來的? –

+0

這是過濾的實際值,在你的情況下,我猜它是FilterKey隱藏字段的值。假設'filterProducts'包含'[{someProperty:'1',name:'obj1'},{someProperty:'2',name:'obj2'}]'並且您這樣做:'

  • {{product.name}}
  • '然後,ng-repeat將自動僅顯示其'someProperty'屬性中具有'2'的產品對象。所以在這種情況下,你只應該看到一個包含「obj2」的LI元素。 – mojarras