gpt4 book ai didi

javascript - AngularJS:具有不同属性的数组数组中的 ngIf

转载 作者:行者123 更新时间:2023-12-03 00:06:51 25 4
gpt4 key购买 nike

我的对象:

objects = [
{
name: "1",
foo: [{
value: 0.5,
ignored: null,
text: "foo1"
}, {
value: 0,
ignored: null,
text: "foo"
}]
},
{
name: "2",
foo: [{
value: 0,
ignored: null,
text: "foo"
}, {
value: 0.5,
ignored: null,
text: "foo2"
}]
},
{
name: "3",
foo: [{
value: 0.5,
ignored: null,
text: "foo3"
}]
},
{
name: "4",
foo: [{
value: 0,
ignored: 1,
text: "foo4"
}]
},
{
name: "5",
foo: [{
value: 0,
ignored: null,
text: "foo5"
}]
}]

我的 AngularJs 模板 html:

<div class="row" ng-repeat="object in objects">
{{ object.name }}
<div ng-repeat="foo in object.foo">
<div ng-if="foo.value > 0 || foo.ignored == 1">
{{ foo.text }}
</div>
</div>
</div>

我的结果是:

1 foo1
2 foo2
3 foo3
4 foo4
5

但我不想显示案例 N°5 的 object.name

如何在第一个ng-repeat中使用第二个ng-repeatng-if

<div class="row" ng-repeat="object in objects" ng-if="...???...">
{{ object.name }}
any text !!
</div>

最佳答案

您可以在ng-if中使用过滤函数。

更改的模板:

<div class="row" ng-repeat="object in objects" ng-if="myFilter(object)">
{{ object.name }}
<div ng-repeat="foo in object.foo">
<div ng-if="foo.value > 0 || foo.ignored == 1">
{{ foo.text }}
</div>
</div>
</div>

Controller 中的过滤功能:

$scope.myFilter = function(o) {
// Returns a truthy value if the foo-subarray meets the criteria.
var found = o.foo.find(function(fooElement) {
return fooElement.value > 0 || fooElement.ignored == 1;
});
return found;
};

结果:

1
foo1
2
foo2
3
foo3
4
foo4

关于javascript - AngularJS:具有不同属性的数组数组中的 ngIf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54929190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com