2016-05-13 92 views
0

假設有一個數組:
[{name:'a',display:'obj.a ===「1」},{name:' b',display:true}]
是否可以通過屬性顯示的實際值過濾數組?
in angularjs,如何通過包含表達式的屬性過濾數組

angular.module('myApp', []) 
 
.controller('MyController', ['$scope', function ($scope) { 
 
    $scope.obj = {}; 
 
    $scope.arr = [{name: 'a', display: 'obj.a==="1"'}, {name: 'b', display: true}]; 
 
}]) 
 

 
     ;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyController"> 
 
    <input type="text" ng-model="obj.a"> 
 
    <div> 
 
     <span ng-repeat="item in arr|filter:{display: true}">{{item.name}}</span> 
 
    </div> 
 
</div>

+0

仍然無法理解u能再次解釋 – Dhiraj

回答

0

希望這有助於你。祝你好運

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyController"> 
 
    <input type="text" ng-model="obj.a"> 
 
    <div> 
 
     <span ng-repeat="item in arr|regex:'display':regex_check">{{item.name}}</span> 
 
    </div> 
 
</div> 
 
<script> 
 
\t app = angular.module('myApp', []); 
 
\t app.controller('MyController', ['$scope', function ($scope) { 
 
\t \t $scope.regex_check = '"[1]"'; 
 
\t $scope.obj = {}; 
 
\t $scope.arr = [{name: 'a', display: 'obj.a==="1"'}, {name: 'b', display: true}]; 
 
\t }]) 
 
\t 
 
\t app.filter('regex', function() { 
 
\t return function(input, field, regex) { 
 
\t \t var patt = new RegExp(regex);  
 
\t \t var out = []; 
 
\t \t for (var i = 0; i < input.length; i++){ 
 
\t \t \t if(patt.test(input[i][field])) 
 
\t \t \t \t out.push(input[i]); 
 
\t \t } 
 
\t \t return out; 
 
\t }; 
 
\t }); 
 
</script>

相關問題