Javascript 如何使用 AngularJs 过滤 JSON 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11923142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to filter JSON-Data with AngularJs?
提问by Vural
I have an array (_users) that contains JSON objects.
我有一个包含 JSON 对象的数组 (_users)。
{
"User":
{
"userid":"19571",
"status":"7",
"active":"1",
"lastlogin":"1339759025307",
"Stats":
[
{
"active":"1",
"catid":"10918",
"typeid":"71",
"Credits":
[
{
"content":"917,65",
"active":"1",
"type":"C7"
},
{
"content":"125,65",
"active":"1",
"type":"B2"
}
]
}
]
}
}
1- How can I filter only users with "active":"1" not "0"
1-如何仅过滤具有“活动”的用户:“1”而不是“0”
I have tried something like this:
我试过这样的事情:
<div ng-repeat="user in _users | filter:active | orderBy:user.userid">
{{user.userid}}
</div>
but not working correctly for me.
但对我来说不能正常工作。
Thank you!
谢谢!
回答by pkozlowski.opensource
Since your object model is kind of complex I would recommend using custom filtering function:
由于您的对象模型有点复杂,我建议使用自定义过滤功能:
$scope.isActive = function(user) {
return user.User.Stats[0].active === "1";
};
and then in your HTML:
然后在你的 HTML 中:
<div ng-repeat="user in _users | filter:isActive">
{{user.User.userid}}
</div>
Here is the working jsFiddle: http://jsfiddle.net/pkozlowski_opensource/4kzzy/3/
这是工作的 jsFiddle:http: //jsfiddle.net/pkozlowski_opensource/4kzzy/3/
Be sure to check angular's documentation on filters if you need more info: http://docs.angularjs.org/api/ng.filter:filter
如果您需要更多信息,请务必查看关于过滤器的 angular 文档:http: //docs.angularjs.org/api/ng.filter:filter
回答by okigan
Taking @pkozlowski.opensource a bit further and allowing live filtering of items/users:
更进一步地使用@pkozlowski.opensource 并允许对项目/用户进行实时过滤:
<div ng-controller="MyCtrl">
<label>userid substring filter:</label>
<input ng-model="zzzzz">
<div ng-repeat="user in _users | filter:isInteresting">
<pre>{{user | json}}</pre>
</div>
</div>
And:
和:
$scope.isInteresting = function (user) {
if ($scope.zzzzz == undefined) {
return true;
}
return user.userid.indexOf($scope.zzzzz) !== -1;
};