AngularJS 在控制器中过滤 REST JSON
AngularJS Filter REST JSON in Controller
我需要按类别输出 JSON 中的项目计数(我相信使用 .length
),并且想在控制器中管理它,以便我可以将它放在任何我想要的范围内想。如何在控制器中过滤 REST JSON?
我的样本JSON如下:
[
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
"articletitle": "artilce1",
"articlecategoryid": 1,
"articlesummary": "article 1 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
"articletitle": "artilce2",
"articlecategoryid": 2,
"articlesummary": "article 2 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
"articletitle": "artilce3",
"articlecategoryid": 3,
"articlesummary": "article 3 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D7",
"articletitle": "artilce4",
"articlecategoryid": 1,
"articlesummary": "article 3 summary. "
},
]
我的资源如下:
// Articles by ID
pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles', {},
{
'update': { method:'PATCH'}
});
}]);
我的控制器如下:
// Count number of total articles
pfcControllers.controller('pfcArticleCountCtrl', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {
$scope.articlecount = pfcArticles.query();
我认为我需要向控制器添加一些东西(例如 $scope.cat1 = filter(my logic to get my category here)
,但我对如何查询这个有点迷茫所以我有每个类别的项目数。
像这样构建一个过滤器。
angular.module('FilterModule', [])
.filter( 'cat1', function() {
return function(yourJSON) {
var count = 0;
yourJSON.forEach(function(item) {
switch(item.articlecategoryid) {
case 1:
count++;
break;
default:
console.log('item is not of category 1');
}
});
return count;
};
});
注入 $filter 服务并获取 cat1
过滤器。现在您可以将其用作 $scope.cat1 = $filter('cat1')(yourJSON);
我需要按类别输出 JSON 中的项目计数(我相信使用 .length
),并且想在控制器中管理它,以便我可以将它放在任何我想要的范围内想。如何在控制器中过滤 REST JSON?
我的样本JSON如下:
[
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
"articletitle": "artilce1",
"articlecategoryid": 1,
"articlesummary": "article 1 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
"articletitle": "artilce2",
"articlecategoryid": 2,
"articlesummary": "article 2 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
"articletitle": "artilce3",
"articlecategoryid": 3,
"articlesummary": "article 3 summary. "
},
{
"id": "66D5069C-DC67-46FC-8A51-1F15A94216D7",
"articletitle": "artilce4",
"articlecategoryid": 1,
"articlesummary": "article 3 summary. "
},
]
我的资源如下:
// Articles by ID
pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
return $resource('https://myrestcall.net/tables/articles', {},
{
'update': { method:'PATCH'}
});
}]);
我的控制器如下:
// Count number of total articles
pfcControllers.controller('pfcArticleCountCtrl', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {
$scope.articlecount = pfcArticles.query();
我认为我需要向控制器添加一些东西(例如 $scope.cat1 = filter(my logic to get my category here)
,但我对如何查询这个有点迷茫所以我有每个类别的项目数。
像这样构建一个过滤器。
angular.module('FilterModule', [])
.filter( 'cat1', function() {
return function(yourJSON) {
var count = 0;
yourJSON.forEach(function(item) {
switch(item.articlecategoryid) {
case 1:
count++;
break;
default:
console.log('item is not of category 1');
}
});
return count;
};
});
注入 $filter 服务并获取 cat1
过滤器。现在您可以将其用作 $scope.cat1 = $filter('cat1')(yourJSON);