Angular js过滤器不起作用?

时间:2017-03-21 11:21:35

标签: angularjs angularjs-filter

请查看输出并说明为什么它会像这样工作? 我试图将过滤器用于嵌套值,过滤器提供随机输出。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter:x.na.foo=0">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>


</body>
</html>

,输出为:

在输入字段中输入字母:

Jani 0
Margareth 1
Hege 0
Joe 1
Gustav 0
Mary 0
Kai 1
The list will only consists of names matching the filter.

3 个答案:

答案 0 :(得分:2)

在过滤嵌套对象时使用这样的过滤器

<li ng-repeat="x in names | filter: {na: {foo: 0}}">

演示

angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter: {na: {foo: 0}}">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

 

<p>The list will only consists of names matching the filter.</p>


</body>
</html>

答案 1 :(得分:1)

通过控制器:

&#13;
&#13;
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  
  $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
    
     $scope.foofilter = function(data) { 
        return data.na.foo === 0 ;
   };
});
&#13;
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    
    <p>Type a letter in the input field:</p>

<ul>
  <li ng-repeat="x in names | filter: foofilter">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

  </body>

</html>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我有两个代码来过滤,一个来订购,

<强>排序依据

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<input type="text" ng-model="search"/>

<ul>
  <li ng-repeat="x in names | orderBy:'-na.foo'" ">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>

过滤

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<input type="text" ng-model="search"/>

<ul>
  <li ng-repeat="x in names | filter:search">
    {{ x.name }} {{x.na.foo}}
  </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [    
        {name :'Jani' , sName : 0, na :{foo : 0} },
        {name :'Carl' , sName : 1 , na :{foo :1}},
        {name :'Margareth' , sName : 0 , na :{foo : 1}},
        {name :'Hege' , sName : 0, na :{foo : 0} },
        {name :'Joe' , sName : 0 , na :{foo : 1}},
        {name :'Gustav' , sName : 1, na :{foo : 0} },
        {name :'Birgit' , sName : 1 , na :{foo : 1}},
        {name :'Mary' , sName : 0 , na :{foo : 0}},
        {name :'Kai' , sName : 0, na :{foo : 1} }
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>

请使用可以帮助您的那个