angular - Angular:组件不会使用来自公共服务变量的数据进行更新
问题描述
由于这个应用程序中的项目并不多Source
,我想我会很聪明地设计一个服务,它也充当存储库,并在服务添加、更新或删除项目时在内部进行更新。
为了在第一次使用时填充存储库,我的主要组件然后listSources()
通过订阅它运行一次。所有其他组件都注入了服务,并且在它们的模板中我let source of sourceService.sources
用来获取源代码。
但是,当我通过addSource()
或任何其他方式更改源时,这些更改不会反映在我的其他组件中。我也尝试过使用一个主题,但这变得更加混乱并且对我也不起作用。
有人知道我在做什么错吗?
这是服务:
export class SourceService { // truncated for brevity
public sources: Source[] = [];
constructor( ) { }
public listSources(): Observable<Source[]> {
// if sources are already loaded, just return from memory
if (this.sources.length > 0) {
return of(this.sources);
}
return this.http.get<Source[]>(sourceUrl, httpOptions).pipe(
tap(sources => {
sources.forEach((source, index, sources) => {
this.sources.push(source);
});
});
}
public addSource(source: Source): Observable<Source> {
return this.http.post<Source>(sourceUrl, source, httpOptions).pipe(
tap(data => {
// update memory
this.sources.push(Object.assign(new Source(), Source.EMPTY_MODEL));
})
);
}
在其他地方,已sourceService
注入的组件具有以下模板:
<mat-option *ngFor="let source of sourceService.sources" [value]="source.id">{{ source.title }}</mat-option>
解决方案
尝试这个
export class SourceService { // truncated for brevity
public sources: Source[] = [];
public sources$ = new BehavoiurSubject<Source[]>([]);
constructor( ) { }
public listSources(): Observable<Source[]> {
// if sources are already loaded, just return from memory
if (this.sources.length) {
this.sources$.next(this.sources);
return of(this.sources);
}
return this.http.get<Source[]>(sourceUrl, httpOptions).pipe(
tap(sources => {
sources.forEach((source, index, sources) => {
this.sources.push(source);
});
this.sources$.next(this.sources);
});
}
public addSource(source: Source): Observable<Source> {
return this.http.post<Source>(sourceUrl, source, httpOptions).pipe(
tap(data => {
// update memory
this.sources.push(Object.assign(new Source(), Source.EMPTY_MODEL));
this.sources$.next(this.sources);
})
);
}
在你的组件中
<ng-container *ngIf="sourceService.sources$ | async as sources>
<mat-option *ngFor="let source of sources" [value]="source.id">{{ source.title }}</mat-option>
</ng-container >
推荐阅读
- cmake - CMake查找项目中兄弟路径中定义的函数
- python-3.x - DRF auth_token "non_field_errors": [ "无法使用提供的凭据登录。" ] 使用自定义用户模型
- azure - Azure ML Studio 中的 Evaluate Model Node 不会在混淆矩阵中获取数据集的所有行
- azure-devops-rest-api - 如何使用 HttpRepl 进行身份验证以测试受保护的 api?
- math - 如果我在图像上有矩形,然后将图像旋转 90 度,那么矩形的新坐标是多少?
- google-apps-script - 当一个或多个单元格在一个范围内发生变化时运行触发脚本
- ffmpeg - Webm 是否支持封面艺术?
- data-structures - Dijkstra的最短路径算法优化
- python - 使用 tf.keras.metrics.Accuracy 时,“不允许使用 tf.Tensor 作为 Python bool”
- powerbi - powerbi 表中两列与 dax 的总和乘积