在 ngRepeat 中筛选嵌套属性

Filtering Nested Properties in ngRepeat

本文关键字:嵌套 属性 筛选 ngRepeat      更新时间:2023-09-26

在我的控制器中,我有:

$scope.currentDiscount = new DiscountPrototype;

哪里:

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
}

然后我把一堆新的DiscountProductPrototypes推到$scope.currentDiscount的DiscountProducts数组中。如果折扣产品具有产品类别类型 = "C",则它是一个类别,如果它是"P",则它是一个产品。我的过滤器打算只显示折扣产品数组中作为类别的对象。

<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{category.ProductCategoryType: 'C' : true}"
     class="productTile">
  <p>{{category.Name}}</p>
</div>

使用上面的过滤器,我得到以下错误:

Syntax Error: Token '.' is at column {2} of the expression [{3}] starting at [{4}].

这似乎说有问题:

category.ProductCategoryType

如果我执行以下操作,则不会出错,但过滤器不执行任何操作。将显示类别和产品。

<div ng-repeat="category in currentDiscount.DiscountProducts | filter: category.ProductCategoryType='C'" 
     class="productTile">

是否需要创建自定义筛选器才能实现此类筛选?

这应该有效:

<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{ProductCategoryType: 'C'}"
     class="productTile">
  <p>{{category.Name}}</p>
</div>

问题在于true单词的位置。 true必须放在第二个冒号之后。在第一个冒号之后,它应该只是Expression.

尝试以下...

<div ng-repeat="category in currentDiscount.DiscountProducts | filter:{ProductCategoryType: 'C'}: true"
     class="productTile">
  <p>{{category.Name}}</p>
</div>

有关筛选器的详细信息,请参阅:https://docs.angularjs.org/api/ng/filter/filter