繁体   English   中英

过滤ngRepeat中的嵌套属性

[英]Filtering Nested Properties in ngRepeat

在我的控制器中,我有:

$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数组中。 如果DiscountProduct的ProductCategoryType =“ C”,则为类别,如果为“ P”,则为产品。 我的过滤器仅打算显示DiscountProduct数组中属于类别的对象。

<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">

我是否需要创建自定义过滤器才能实现这种过滤?

问题在于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

这应该工作:

 <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.

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