如何使用 AngularJs 过滤 JSON 数据? [英] How to filter JSON-Data with AngularJs?

查看:25
本文介绍了如何使用 AngularJs 过滤 JSON 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 JSON 对象的数组 (_users).

<代码>{用户":{"userid":"19571","状态":"7","活动":"1","lastlogin":"1339759025307",统计":[{"活动":"1","catid":"10918","typeid":"71",学分":[{"内容":"917,65","活动":"1",类型":C7"},{"内容":"125,65","活动":"1",类型":B2"}]}]}}

1- 如何仅过滤具有 "active":"1" 而非 "0" 的用户

我尝试过这样的事情:

{{user.userid}}

但不适合我.

谢谢!

解决方案

由于您的对象模型有点复杂,我建议您使用自定义过滤功能:

$scope.isActive = function(user) {返回 user.User.Stats[0].active === "1";};

然后在您的 HTML 中:

{{user.User.userid}}

这是正在运行的 jsFiddle:http://jsfiddle.net/pkozlowski_opensource/4kzzy/3/>

如果您需要更多信息,请务必查看 angular 关于过滤器的文档:http://docs.angularjs.org/api/ng.filter:filter

I have an array (_users) that contains JSON objects.

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

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!

解决方案

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:

<div ng-repeat="user in _users | filter:isActive">
    {{user.User.userid}}
</div>

Here is the working 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

这篇关于如何使用 AngularJs 过滤 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆