[英]Angular 9 data from api call isn't populating dropdown
Having some problems with filling my drop down menu with data from my API.用我的 API 中的数据填充下拉菜单时遇到一些问题。 I need help figuring out what I am doing wrong because I am getting no errors in the console.我需要帮助找出我做错了什么,因为我在控制台中没有收到任何错误。
Service.ts method call: Service.ts 方法调用:
@Injectable({
providedIn: 'root'
})
export class OrderExceptionReportService extends ApiServiceBase {
private apiRoute: string = 'exception-report';
public sessionData: ExceptionReportSessionData[];
constructor(http: HttpClient, configService: ConfigService) {
super(http, configService);
}
public async GetExceptionReportSessionData(): Promise<ExceptionReportSessionData[]> {
if (!this.appSettings) {
this.appSettings = await this.configService.loadConfig().toPromise();
}
return await this.http.get<ExceptionReportSessionData[]>(this.appSettings.apiSetting.apiAddress + this.apiRoute + '/session-data')
.pipe(
retry(1),
catchError(this.handleError)
)
.toPromise()
}
}
component.ts file:组件.ts 文件:
@Component({
selector: 'app-order-exception-report',
templateUrl: './order-exception-report.component.html',
styleUrls: ['./order-exception-report.component.scss']
})
export class OrderExceptionReportComponent implements OnInit {
public sessionData: ExceptionReportSessionData[];
constructor(private orderExceptionReportService: OrderExceptionReportService) {
}
getExceptionReportSessionData() {
this.orderExceptionReportService.GetExceptionReportSessionData()
.then(
data => {
this.sessionData = data;
});
}
ngOnInit() {
}
}
html where I need the data: html 我需要数据的地方:
<div class="input-group">
<select class="custom-select" id="inputGroupSelect01">
<option value="" *ngFor="let session of sessionData">{{session.SessionName}}</option>
</select>
</div>
Interface.ts file:接口.ts 文件:
export interface ExceptionReportSessionData {
SessionName: string,
ReportFiles: Array<string>
}
From your comments, it appears the this.configService.loadConfig()
returns an HTTP observable as well.从您的评论来看, this.configService.loadConfig()
似乎也返回了一个 HTTP 可观察值。
In that case, you could avoid using promises and streamline the process using RxJS method iff
to check if the this.appSettings
variable is defined and switchMap
operator to switch to the target request once appSettings
is retrieved.在这种情况下,您可以避免使用承诺并使用 RxJS 方法iff
来简化流程,以检查是否定义了this.appSettings
变量,并在检索到appSettings
后使用switchMap
运算符切换到目标请求。
Try the following尝试以下
public GetExceptionReportSessionData(): Observable<ExceptionReportSessionData[]> {
return iff(() =>
this.appSettings,
of(this.appSettings),
this.configService.loadConfig().pipe(tap(appSettings => this.appSettings = appSettings))).pipe(
switchMap(appSettings => this.http.get<ExceptionReportSessionData[]>(appSettings.apiSetting.apiAddress + this.apiRoute + '/session-data')),
retry(1),
catchError(this.handleError)
);
}
tap
operator is used to tap into the result and store it into this.appSettings
variable. tap
运算符用于挖掘结果并将其存储到this.appSettings
变量中。of
method is used to send an observable of this.appSettings
since switchMap
operator expects an observable.of
方法用于发送this.appSettings
的 observable,因为switchMap
运算符需要一个 observable。
You could then subscribe to the function in the component然后您可以在组件中订阅 function
export class OrderExceptionReportComponent implements OnInit {
public sessionData: ExceptionReportSessionData[];
constructor(private orderExceptionReportService: OrderExceptionReportService) {}
getExceptionReportSessionData() {
this.orderExceptionReportService.GetExceptionReportSessionData().subscribe(
data => { this.sessionData = data; },
error => { }
);
}
ngOnInit() {
this.getExceptionReportSessionData();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.