2017-10-11 62 views
0

對於使用角度濾波器濾波JavaScript數組的語法是具有角濾波器或condtion

$濾波器(「過濾器」)(陣列,表達,比較器,anyPropertyKey)

作爲每角文檔第二參數表達可以採取

甲圖案對象來過濾由陣列包含的對象 特定屬性。例如{名稱:「M」,電話:「1」}謂詞將 返回具有含「M」,並在提及例如包含「1」

屬性電話屬性名項的陣列,我們越來越情況如姓名匹配「M」 手機匹配的「1」

,但如何實現條件爲名稱匹配「M」 手機匹配「1」

代碼示例

var employees=[ 
    {lastname:"john",firstname:"jack",age:40,sex:"m"}, 
    {lastname:"Abby",firstname:"john",age:20,sex:"f"}, 
    ]; 

var filteredData =$filter('filter')(employees,{firstname:'john', lastname:'john'}); 

這導致了因爲沒有記錄和應用 可我們試圖篩選具有姓氏或名字爲「約翰」

這是可能實現與使用了自定義過濾器

+0

顯示您的代碼和數據 – jitender

+1

的可能的複製[AngularJs與 「或」 條件過濾?(https://stackoverflow.com/questions/39554350/angularjs-filter-with-or-condition) –

+0

@ jitender更新了問題 – lrvkiran

回答

0

var myApp = angular.module('myApp', []); 
 

 
myApp.controller('myCtrl', ['$scope', '$filter', function($scope, $filter){ 
 

 
    var employees=[ 
 
    {lastname:"john",firstname:"jack",age:40,sex:"m"}, 
 
    {lastname:"Abby",firstname:"john",age:20,sex:"f"}, 
 
    {lastname:"Abby",firstname:"mark",age:20,sex:"f"}, 
 
    ]; 
 
    
 
$scope.filteredData = $filter('filter')(employees, function(value){ 
 
     return value.firstname == 'john' || value.lastname == 'john'; 
 
}); 
 

 
console.log($scope.filteredData); 
 
    
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<!DOCTYPE html> 
 
<html ng-app="myApp"> 
 

 
<body> 
 

 
    <div ng-controller="myCtrl"> 
 
    {{filteredData}} 
 
    </div> 
 

 

 
</body> 
 

 
</html>
記錄

你可以像下面一樣使用。

$scope.filteredData = $filter('filter')(employees, function(value){ 
     return value.firstname == 'john' || value.lastname == 'john'; 
});