【发布时间】:2019-09-10 02:29:50
【问题描述】:
我正在尝试根据从 observable 返回的值对数组元素进行分组,并在视图上将这些组显示为不同的 <mat-card>。
我有一个 in-memory-web-api-module,其中包含下面简化示例的类。
我还在学习 Angular,所以请原谅我的菜鸟
例如: in-memory-data.service.ts
export class InMemoryDataService implements InMemoryDbService {
createDb(){
const apps = [
{id: '1', app: 'app1', env_id:2, env:'DEV'},
{id: '2', app: 'app2' env_id:2, env:'DEV'},
{id: '4', app: 'app3' env_id:2, env:'DEV'},
{id: '5', app: 'app1' env_id:3, env:'Test'},
{id: '6', app: 'app2' env_id:3, env:'Test'},
{id: '7', app: 'app3' env_id:1, env:'PROD'},
];
return {apps}
}
应该为每个不同的应用程序值创建一个新的<mat-card>,而不是为每个应用程序值创建它现在正在做的事情。
这是我的代码。
applications.service.ts 编辑:
import { Injectable } from '@angular/core';
import { Apps } from './applications';
import { APPS } from './mock-applications';
import { Observable, of } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { MessagesService } from './messages.service';
import { catchError, map, switchMap, tap, groupBy, mergeMap } from 'rxjs/operators';
export class ApplicationsService {
private applicationUrl = 'api/apps';
constructor(private messageService: MessagesService, private http: HttpClient ) { }
getApps(): Observable<Apps[]> {
return this.http.get<Apps[]>(this.applicationUrl)
.pipe(
groupBy(application=> application.app),
mergeMap(obs => {
retun obs.pipe(
toArray(),
map(apps => {
return {id: obs.key, group: apps}
})
)
}),
toArray(),
tap(_ => this.log('fetched apps')),
catchError(this.handleError<Apps[]>('getApps', []))
)
}
/*Type 'Observable<Apps[] | {}[]>' is not assignable to type 'Observable<Apps[]>'.
Type 'Apps[] | {}[]' is not assignable to type 'Apps[]'.
Type '{}[]' is not assignable to type 'Apps[]'.
Type '{}' is missing the following properties from type 'Apps': guid, mots_spi_indicator, mots_sox_indicator, mots_pci_indicator, and 42 more./*
getApp(id: string): Observable<Apps> {
const url = `${this.applicationUrl}/${id}`;
return this.http.get<Apps>(url).pipe(
tap(_ => this.log(`fetched app guid=${id}`)),
catchError(this.handleError<Apps>(`getApp guid=${id}`))
);
}
}
home.component.ts
import { Component, OnInit } from '@angular/core';
import { Apps } from '../../applications'
import { APPS } from '../../mock-applications';
import { ApplicationsService } from 'src/app/applications.service';
export class HomeComponent implements OnInit {
apps: Apps[];
constructor(private appService: ApplicationsService) { }
ngOnInit() {
this.getApps();
}
getApps(): void{
this.appService.getApps()
.subscribe(apps => this.apps = apps);
}
}
home.component.html
<h1>My Access</h1>
<app-app-search></app-app-search>
<div fxLayout="row wrap">
<div *ngFor="let app of apps" fxFlex.gt-sm="25" fxFlex.gt-xs="50" fxFlex="100">
<a routerLink="/detail/{{app.id}}">
<mat-card class="card-widget mat-teal">
<div mat-card-widget>
<div mat-card-float-icon>
<mat-icon>error</mat-icon>
</div>
<div class="pl-0">
<h2 mat-card-widget-title>{{app.app}}</h2>
</div>
</div>
</mat-card>
</a>
</div>
</div>
我怎样才能使每个应用名称都放入它自己的组中?
【问题讨论】:
标签: angular typescript rxjs angular7