Angular js 过滤器不起作用?

Angular js filter is not working?

请查看输出并给出它为什么这样工作的原因?? 我正在尝试将过滤器用于嵌套值,而过滤器正在提供随机输出。

<!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.

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

<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>

通过控制器:

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 ;
   };
});
<!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>

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

排序方式

    <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>

请使用对你有帮助的那个