[英]How do I pass the DOM object (this) that contains an ng-repeat statement into the ng-if function? (Javascript/AngularJS)
I have an ng-repeat on my page, and I need to filter the items that are in it. 我的页面上有一个ng-repeat,我需要过滤其中的项目。 For the purposes of filtering I've included the hidden field above it which contains a key that I need to use.
为了进行过滤,我在其上方包括了一个隐藏字段,其中包含我需要使用的密钥。
<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 ends up being null, though. 但是$ event最终为null。
Try using the filter directly in ng-repeat 尝试直接在ng-repeat中使用过滤器
<li ng-repeat="product in filterProducts | filter: {id: '<FilterKeyValue>'}" >
Where id
is the property name of your product
object you want use to filter by. 其中
id
是您要用来过滤的product
对象的属性名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.