[英]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.