根据孩子的状态角度更改家长状态/班级

时间:2018-09-11 15:26:49

标签: javascript angular

如果所有孩子的状态都为“ fail”(我使用的是角度5),我想更改父类。

我的JSON:

[{"parent":"PARENT 1", "child":[{ "name":"Child 1", "status":"fail" }, { "name":"Child 1", "status":"fail" },{ "name":"Child 1", "status":"fail" }, { "name":"Child 1", "status":"fail" }], "someVal":"SOME VALUE" }, { "parent":"PARENT 2", "child":[{ "name":"Child 1", "status":"pass" }, { "name":"Child 1", "status":"pass" },{ "name":"Child 1", "status":"fail" }, { "name":"Child 1", "status":"fail" }], "someVal":"SOME VALUE" }, { "parent":"PARENT 3", "child":[{ "name":"Child 1", "status":"pass" }, { "name":"Child 1", "status":"pass" },{ "name":"Child 1", "status":"pass" }, { "name":"Child 1", "status":"pass" }], "someVal":"SOME VALUE" }]

我的HTML:

<ul>
<li *ngFor="let anyvar of mainJSONOBJ" [ngClass]="FAIL/PASS"><a href="">{{anyvar.parent}}</a> 
    <ul *ngFor="let anyvar1 of anyvar.child" >
        <li *ngIf="anyvar1.child"><a href="#">{{anyvar.child.name}}</a></li>
    </ul>
</li>

我要尝试的是,如果所有孩子的父状态LI中的“状态”为“失败”,则将“ ngClass”更改为“失败”,否则将“ ngClass”更改为“通过”。

有没有办法我可以用角度5做到这一点。谢谢。

1 个答案:

答案 0 :(得分:0)

[ngClass]="isFailed(anyvar) ? 'fail' : 'pass'"
isFailed(value: { child: [{ status: string }] }): boolean {
    return value.child.every(c => c.status === 'fail');
}

https://stackblitz.com/edit/angular-lkuthv