AngularJS:返回"子集"来自工厂的数据

时间:2014-04-20 22:10:44

标签: angularjs angularjs-scope

我有这个工厂:

(function(){

    var requestsFactory = function(){

        var requests = [
          {id: 1, title: 'First Request', status: 'In Progress'},
          {id: 2, title: 'Second Request', status: 'Closed'},
          {id: 3, title: 'Third Request', status: 'Waiting'},
          {id: 4, title: 'Fourth Request', status: 'Closed'},
          {id: 5, title: 'Fifth Request', status, 'In Progress'}
        ];

        var factory = {};

        factory.getAllRequests = function(){ return requests; };
        /* THIS IS NEW */
        factory.getOpenRequests = function(){
          var open_requests = filter(requests.status, function(status){
            return status !== 'Closed';
          });
          return open_requests;
        };
        /* END NEW */

       return factory;
}; // end requestsFactory

        return factory;
    };

    angular.module('appITI').factory('requestsFactory', requestsFactory);
}());

这个控制器:

(function(){

    var UsersController = function($scope, usersFactory, requestsFactory){
        $scope.current_user = [];
        $scope.requests = [];
        $scope.open_requests = []; // NEW

        $scope.prefs = 'false';

        function init(){
            $scope.current_user = usersFactory.getCurrentUser();
            $scope.requests = requestsFactory.getAllRequests();
             $scope.open_requests = requestsFactory.getOpenRequests(); //NEW
        }

        init();

    }; // end UsersController

    UsersController.$inject = ['$scope', 'usersFactory', 'requestsFactory'];

    angular.module('appITI').controller('UsersController', UsersController);

}());

我需要在工厂添加一个方法,以便我可以在控制器中调用类似的东西:

$scope.open_requests = requestsFactory.requestsByStatus('In Progress');

本周末我一直专注于学习AngularJs,我似乎无法弄清楚我认为简单的JavaScript是什么。

NEW 我在模板中有这个,现在页面是白色的并且坏了。

<tr class="landing_data_row" data-ng-repeat="request in open_requests | filter:techFilter | orderBy:sortBy:reverse">

1 个答案:

答案 0 :(得分:1)

向工厂添加新功能,并使用filter功能。

factory.getOpenRequests = function(){
  return requests.filter(function(el){
    return el.status !== 'Closed';
  });
};

<强>更新 以下是您更新后的问题Plunker