早上好。我试图获得一个选择框,以在我的文件阵列旁边显示公司列表。最终将用于按公司过滤数组。
我觉得我应该可以使用,但是它现在只显示一个空白页面,带有菜单栏...
ts
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Datafile, DatafileLoader } from '../datafile-loader.service';
import { Company, CompanyLoader } from '../../company/company-loader.service';
@Component({
selector: 'app-datafile-list',
templateUrl: './datafile-list.component.html',
styleUrls: ['./datafile-list.component.scss']
})
export class DatafileListComponent {
list: Observable<Datafile[]>;
listC: Observable<Company[]>;
constructor(loader: DatafileLoader, loaderc: CompanyLoader) {
this.list = loader.getList();
this.listC = loaderc.getList();
}
}
html
<mat-card class="dashboard-card">
<mat-card-header [style.backgroundColor]="'orange'">
<mat-card-title>Datafiles</mat-card-title>
</mat-card-header>
<mat-card-content [ngStyle]="{'height':'500px', 'overflow-y': 'auto', 'background-color': 'white'}" >
<div *ngFor="let l of list | async">
<p style="line-height:1">
<span> <img src="{{l.layoutPreDefined == false ? '../../../assets/images/newlayout.jpg' : '../../../assets/images/predefinedlayout.jpg'}}"/></span><br />
<span style="font-size:12pt"> <a [routerLink]="l.id"> {{ l.fileName }} </a> </span><br />
<span style="font-size:12pt"> {{ l.companyAbbreviation}} {{ l.fileTypeName }}</span><br />
<img src="../../../assets/images/divider.jpg">
</p>
</div>
</mat-card-content>
</mat-card>
<mat-form-field>
<mat-select>
<mat-option *ngFor="let c of listC | async" [value]="c">{{c.companyAbbreviation}}</mat-option>
</mat-select>
</mat-form-field>
公司装载机
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
const apiUrl = 'http://xxxxxx/DRMSAPI-dev/API/company';
export interface Company {
id?: string;
name: string;
companyAbbreviation: string;
}
@Injectable()
export class CompanyLoader {
constructor(private http: HttpClient) {}
getList(): Observable<Company[]> {
return this.http.get<Company[]>(apiUrl).pipe(map(longList => longList.slice(0, 9)), tap(results => console.log(results)));
}
getDetails(id: string): Observable<Company> {
return this.http.get<Company>(
`${apiUrl}/${id}`
);
}
}
数据文件模块
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DatafileDetailComponent } from './datafile-detail/datafile-detail.component';
import { DatafileListComponent } from './datafile-list/datafile-list.component';
import { DatafileLoader } from './datafile-loader.service';
import { EDXMaterialModule } from '../shared/edxMaterialModule';
import { FlexLayoutModule } from '@angular/flex-layout';
import { CompanyLoader } from '../company/company-loader.service';
const routes: Routes = [
{ path: '', component: DatafileListComponent },
{ path: ':id', component: DatafileDetailComponent }
];
@NgModule({
declarations: [DatafileListComponent, DatafileDetailComponent],
imports: [
CommonModule,
HttpClientModule,
RouterModule.forChild(routes),
EDXMaterialModule,
FlexLayoutModule
],
providers: [DatafileLoader, CompanyLoader]
})
export class DatafileModule { }
我将CompanyLoader添加到datafile.module的提供程序中,现在我看到选择器和卡了,但是它们都是空白...叹气...
答案 0 :(得分:0)
我不知道您的服务如何,但是如果您需要连接到Api,则需要使用Observable来获取数据。
您可以在以下链接中看到它
https://angular.io/guide/observables-in-angular
对不起,我需要您的服务才能回答您。