我有一个分层的数据模型,包含产品线,然后是子行,然后是子行的子行等。我试图做的是只隔离直接后代的子行(儿子不是孙子女)的特定行或子行。分层数据模型的角度嵌套过滤器
这里是我现有的数据模型:
items:[
{
"_id": "1",
"description": "sth1",
"name": "smname1",
"level_type": "line",
"ancestor": "",
"descendant": "smname2"
}
},
{
"_id": "2",
"description": "sth2",
"name": "smname1",
"level_type": "subline",
"ancestor": "smname1",
"descendant": ""
}
},
]
而且上面还有一件事,我试图完成的例子是让所有产品线的孩子。我曾尝试,但至今没有工作是:
控制器
$scope.prodClassIsALine = function(item) {
return item.level_type=='line';
};
$scope.prodClassIsASubLineof = function(item) {
return item.ancestor==$scope.prodClassIsALine.name;
};
悲剧的建议只是为了告诉你,我需要所有线路的所有儿童,即与是项祖名的所有项目线。
的Html
<div ng-repeat="item in items | filter:prodClassIsALine:prodClassIsASubLineof">
<p>{[{item.name}]}</p>
</div>
这是我们在AngularJS嵌套过滤器的方法?看起来,过滤器遍历列表,作为属性给出的列表,而不是我无法详细了解它们的工作方式。请帮忙。
解决方案
在的script.js
//product is my ng-module
//filter to get all product classes that are lines
product.filter('prodClassIsALine', function() {
return function(input) {
var out = [];
for (var i = 0; i < input.length; i++) {
if (input[i].level_type=='line') {
out.push(input[i])
};
};
return out;
};
});
//filter to get all children of product classes that are lines
product.filter('prodClassLineChild', function() {
return function(input) {
var out = [];
var out2 = [];
for (var i = 0; i < input.length; i++) {
if (input[i].level_type=='line') {
out2.push(input[i])
};
};
for (var i = 0; i < out2.length; i++) {
for (var j = 0; j < input.length; j++) {
if (input[j].ancestor==out2[i].name) {
out.push(input[j])
};
};
};
return out;
};
});
的Html
<div ng-repeat="item in items | prodClassIsALine">
<!-- or <div ng-repeat="item in items | prodClassLineChild"-->
<p>{[{item.name}]}</p>
</div>
不起作用。 – pv1
感谢您的帮助bresleveloper – pv1
欢迎我的朋友 – bresleveloper