Angular 9-获取对象的Observable Array内部的值



我有一个服务器对象的数组,在该数组中我有另一个可观察的对象数组,其关键是[securityGroups]

我有另一个securitygroupsArray数组,在这里我获取API以获取所有安全组

我需要在securityGroups键中的服务器阵列中查找该服务器上安全组的所有名称,并在选项中仅显示其他阵列(securityGroupArray(的ngfor的名称。我试着用3ngFor,但没用。你能帮我吗?

我的组件代码:

ngOnInit(): void {
forkJoin(
this.serverService.getServer(),
this.securityGroupService.getSecurityGroups())
.pipe(takeWhile(() => this.alive))
.subscribe(([servers, groups]) => {
this.servers = servers.map((item) => ({
...item,
securityGroups: this.serverService.getServerById(item.id)
.pipe(map(server => server["security_groups"]))
}))
this.securityGroupArray = groups['security_groups'].map((item) => ({
...item,
expanded: false,
}))
}

我的html代码:

<div class="form-group" [formGroup]="form">
<div class="col-sm-12">
<select
class="form-control border-primary"
formControlName="server"
>
<option [value]="''">select</option>
<span *ngFor="let group of securityGroupArray">
<option *ngFor="let server of servers" [value]="server.id">
<span *ngFor="let secGroup of server.securityGroups | async">
{{ secGroup.name !== group.name ? server.name : ''}}
</span>
</option>
</span>
</select>
</div>
</div>

我的服务器阵列:

{id: "1879f47f-1c5e-464b-bb76-e7cc13ef426e", name: "hello", flavor: {…}, securityGroups: Observable}
,
{id: "b9c7e32a-bf99-4250-83cb-13523f9c1604", name: "test01", flavor: {…}, securityGroups: Observable}

我的安全GroupArray

{id: "b339774a-9ff7-4136-a0ca-94dd998b8dcc", name: "hello", description: "", …}
,
{id: "e96d271c-aa69-4a61-b0ca-63bfa8c1293e", name: "new09", description: "", …}

<span>不是<select>的有效子级,请改用<ng-container>

<div class="form-group" [formGroup]="form">
<div class="col-sm-12">
<select
class="form-control border-primary"
formControlName="server"
>
<option [value]="''">select</option>
<ng-container *ngFor="let group of securityGroupArray">
<option *ngFor="let server of servers" [value]="server.id">
<span *ngFor="let secGroup of server.securityGroups | async">
{{ secGroup.name !== group.name ? server.name : ''}}
</span>
</option>
</ng-container>
</select>
</div>
</div>

CCD_ 4是";虚拟的";DOM元素;它将不会被呈现为实际的DOM。

最新更新