[英]Filtering Nested Properties in ngRepeat
In my controller I have: 在我的控制器中,我有:
$scope.currentDiscount = new DiscountPrototype;
Where: 哪里:
var DiscountPrototype = function(){
this.Id = 0;
this.PromoCode = null;
this.DiscountValue = null;
this.DiscountProducts = []
}
var DiscountProductPrototype = function(discountId,id,catId,catType,name) {
this.DiscountId = discountId;
this.ProductCategoryType = catType;
this.Name = name
}
And then I push a bunch of new DiscountProductPrototypes into the DiscountProducts array of $scope.currentDiscount. 然后,我将一堆新的DiscountProductPrototypes推入$ scope.currentDiscount的DiscountProducts数组中。 If the DiscountProduct has a ProductCategoryType = "C" it is a category, and if it is "P" it is a product.
如果DiscountProduct的ProductCategoryType =“ C”,则为类别,如果为“ P”,则为产品。 My filter is intending on only displaying the objects in the DiscountProduct array that are categories.
我的过滤器仅打算显示DiscountProduct数组中属于类别的对象。
<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{category.ProductCategoryType: 'C' : true}"
class="productTile">
<p>{{category.Name}}</p>
</div>
With the above filter I get the following error: 使用上面的过滤器,我得到以下错误:
Syntax Error: Token '.' is at column {2} of the expression [{3}] starting at [{4}].
Which seems to say there's something wrong with: 这似乎表明存在问题:
category.ProductCategoryType
If I do the following, I get no error, but the filter does nothing. 如果执行以下操作,则不会出现任何错误,但过滤器不会执行任何操作。 Both categories and products are displayed.
显示类别和产品。
<div ng-repeat="category in currentDiscount.DiscountProducts | filter: category.ProductCategoryType='C'"
class="productTile">
Do I need to create a custom filter to achieve this kind of filtering? 我是否需要创建自定义过滤器才能实现这种过滤?
The problem is with placement of true
word. 问题在于
true
单词的位置。 true
must be placed after second colon. 必须在第二个冒号之后放置
true
。 After first colon, it should be just Expression
. 在第一个冒号之后,应该只是
Expression
。
Try following... 尝试关注...
<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{ProductCategoryType: 'C'}: true"
class="productTile">
<p>{{category.Name}}</p>
</div>
For more information on Filters, refer: https://docs.angularjs.org/api/ng/filter/filter 有关过滤器的更多信息,请参见: https : //docs.angularjs.org/api/ng/filter/filter
This should work: 这应该工作:
<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{ProductCategoryType: 'C'}" class="productTile"> <p>{{category.Name}}</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.