0
返回數據的「子集」我有這樣的工廠:AngularJS:從工廠
(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。
新 我在模板中有這個,現在頁面是白色的和破碎的。
<tr class="landing_data_row" data-ng-repeat="request in open_requests | filter:techFilter | orderBy:sortBy:reverse">
是Angular的「過濾器」嗎?因爲,如果是這樣,我已經給用戶一個文本框來過濾數據以輕鬆找到他們正在尋找的東西......它們會衝突嗎? – jgravois
不,標準的JavaScript功能。 – aludvigsen
廢話!我的大腦充滿了AngularJS本週末的視頻,書籍和課程,我幾乎沒用......謝謝! ...另一件事,我可以把switch語句放在那裏,這樣我就不必爲Status上的每個調用重寫一個函數了嗎?狀態選擇是有限的7種可能性。 – jgravois