2017-03-07 44 views
1

我已自定義過濾器:如何在過濾Angular JS中傳遞兩個參數?

.filter('inArray', function() { 

     return function(input, array) { 
     console.log(input); 
     if(array.indexOf(input)!== -1){ 
       return "red"; 
      } else { 
       return ""; 
     } 
     }; 
    }) 

我試過的類屬性使用此:

class="{{inArray | filter:3}}" 

但它是無聲的,當我做console.log(input);過濾器內

回答

2

我已經得到了它的工作是這樣的:

(function() { 
 

 
    angular 
 
    .module("app", ["ui.bootstrap"]); 
 

 
    angular 
 
    .module("app") 
 
    .controller("AppController", AppController); 
 

 
    AppController.$inject = ["$scope"]; 
 

 
    function AppController($scope) { 
 
    var vm = this; 
 

 

 
    vm.myArray=["1", "2", "3"]; 
 

 

 
    } 
 
    
 
    angular 
 
    .module("app") 
 
    .filter('inArray', function() { 
 

 
     return function(input, array) { 
 
     console.log(input); 
 
     if(array.indexOf(input)!== -1){ 
 
       return "red"; 
 
      } else { 
 
       return ""; 
 
     } 
 
     }; 
 
    }) 
 

 
})();
<!DOCTYPE html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <link data-require="[email protected]" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
 
    <script data-require="[email protected]" data-semver="2.2.4" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> 
 
    <script data-require="[email protected]" data-semver="3.3.7" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
    <script data-require="[email protected]" data-semver="1.6.1" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script> 
 
    <script data-require="[email protected]*" data-semver="2.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="app.js"></script> 
 
</head> 
 

 
<body ng-controller="AppController as vm"> 
 
    
 
    <h1 style="color:{{'2'| inArray: vm.myArray}}">2 is in the array</h1> 
 
    <h1 style="color:{{'5'| inArray: vm.myArray}}">5 is not in the array</h1> 
 
    
 

 

 
</body> 
 

 
</html>

0

只是通過像其他參數這

class="{{"someInput"| inArray:'param1':'param2'}}"

+0

我還沒有看到控制檯:'{{inArray | filter:item.name:filter}}' – Daniel

+0

你可以發佈JSFiddle嗎? –

+0

我在這裏發佈了所有代碼 – Daniel

相關問題