当active为true时过滤Json数据。我正在使用AngularJS

时间:2014-08-24 23:20:36

标签: angularjs json filter

我有json:

[
    {
        "Id": 10004,
        "PageName": "club",
        "active": "true"
    },
    {
        "Id": 10040,
        "PageName": "qaz",
        "active": "false"
    },
    {
        "Id": 10059,
        "PageName": "jjjjjjj",
        "active": "true"
    }
]

只有当“active”为true时,才能使用AngularJS过滤Json并创建新的Json数据吗? 当我点击输入按钮时!

返回示例:

[
    {
        "PageName": "club",
        "active": "true"
    },
    {
        "PageName": "jjjjjjj",
        "active": "true"
    }
]

2 个答案:

答案 0 :(得分:0)

(function () {
    'use strict';

    angular.module
        .module('some')
        .controller('SomeCtrl', SomeCtrl);

    SomeCtrl.$inject = ['$scope'];

    function SomeCtrl($scope) {

        $scope.tasks = [
            {
                "Id": 10004,
                "PageName": "club",
                "active": "true"
            },
            {
                "Id": 10040,
                "PageName": "qaz",
                "active": "false"
            },
            {
                "Id": 10059,
                "PageName": "jjjjjjj",
                "active": "true"
            }
        ];

        $scope.filterTask = function () {
            angular.foreach($scope.tasks, function (task, key) {
                if (task.active === false) {
                    $scope.tasks.splice(key, 1);
                }
            });
        }
    }

}());

答案 1 :(得分:0)

您可以在控制器中使用$filter

$scope.getActiveOnes = function(){
    $scope.activeOnes = $filter('filter')($scope.pages.active, true);
}

现在,您可以将此功能绑定到ng-button按钮,而{{activeOnes}}内部您将拥有活动页面或其他任何内容