簡體   English   中英

使用$ filter根據數組中對象的屬性進行過濾

[英]Filtering based on properties of objects within array using $filter

假設以下數據:

var roster = [
    {
        id: 1,
        attended: true,
        person: {printName: 'Larry'}},
    {
        id: 2,
        attended: false,
        person: {printName: 'Curly'}},
    {
        id: 3,
        attended: true,
        person: {printName: 'Moe'}}];

我試圖找到出席的數組中的對象計數為真。 我嘗試了以下方法:

rosters.html:

{{ (roster | filter:{attended:true} ).length }}

名冊,controller.js:

checkedInCount: function() {
    return $filter('filter')($scope.roster, attended.true).length;
}

html過濾器按預期工作,在此實例中返回2。 但是,該函數版本遇到錯誤ReferenceError: Can't find variable: attended 我假設函數中缺少一些瑣碎的東西,但是我不確定它是什么。

使用對象作為表達式:

return $filter('filter')($scope.roster, { attended: true }).length;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM