繁体   English   中英

如何将包含ng-repeat语句的DOM对象(此)传递给ng-if函数? (Javascript / AngularJS)

[英]How do I pass the DOM object (this) that contains an ng-repeat statement into the ng-if function? (Javascript/AngularJS)

我的页面上有一个ng-repeat,我需要过滤其中的项目。 为了进行过滤,我在其上方包括了一个隐藏字段,其中包含我需要使用的密钥。

<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;
}

但是$ event最终为null。

尝试直接在ng-repeat中使用过滤器

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

其中id是您要用来过滤的product对象的属性名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM