2016-11-23 68 views
0

我想最基本的過濾器我的名單上,如果某值爲null(而不是數組),那麼過濾出來Angular1過濾器:{場:「!!」}不工作

JSON樣品

{ 
    "runningServices": [ 
    { 
     "servicename":"service1", 
     "previousLocations": null, 
     "cancelReason": null, 
     "delayReason": null 
    }, 
    { 
     "servicename":"service2" 
     "previousLocations": null, 
     "cancelReason": null, 
     "delayReason": { 
     "reason":"pigeons" 
     } 
    }, 

    ] 
} 

我的代碼(控制器中的數據綁定到$ scope.listOfServices = data.runningServices)

<tr ng-repeat="service in listOfServices | filter:{delayReason:'!!'}> 
//stuff 
</tr> 

如果沒有過濾器,一切名單,與過濾我只是得到了一個空白頁面也..試着'!'

回答

2

寫一個小的過濾器(nullFilter在我的例子):

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'World'; 

    $scope.listOfServices = { 
    "runningServices": [ 
     { 
     "servicename":"service1", 
     "previousLocations": null, 
     "cancelReason": null, 
     "delayReason": null 
     }, 
     { 
     "servicename": "service2", 
     "previousLocations": null, 
     "cancelReason": null, 
     "delayReason": { 
      "reason": "pigeons" 
     } 
     } 
    ] 
    } 

    $scope.nullFilter = function(value){ 
    return value.delayReason !== null; 
    }; 
}); 

和HTML:

<div ng-repeat="service in listOfServices.runningServices | filter: nullFilter"> 
    {{service.servicename }} 
</div> 

plunker:https://plnkr.co/edit/pc2TkQh5qlnoF6JqfqZh?p=preview

+0

我不能感謝你足夠努力,你放入這個解決方案。使用你的例子是我需要幫助我解決我的問題,並給我一個很好的介紹客戶過濾器。我只需要篩選器,因爲我已將「運行服務的額外級別」併入控制器的範圍中。再次感謝 – MOLEDesign

+0

很高興幫助你:),請注意,我的答案中的nullFilter只是角JS內核過濾器的第二個參數(而不是它自己的過濾器),我更新了我的plunker並添加了customNullFilter(它是自定義過濾器)及其實現。請享用 :) – Andriy