gpt4 book ai didi

angular - 在 Angular/RXJS 中向对象数组的 Observable 添加/删除对象?

转载 作者:行者123 更新时间:2023-12-04 10:37:15 25 4
gpt4 key购买 nike

目标:将一个对象添加到现有的 Observable 对象数组中。将此反射(reflect)在 DOM 上是最后一步。
NewObject.ts :

export class NewObject {
name: string;
title: string;
}

这是 example.component.ts :
import { Observable } from 'rxjs';
import { Component, OnInit, Inject, EventEmitter } from '@angular/core';
import { NewObject } from 'objects';

@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {

// Initializing the object array Observable from a service (step 1)
readonly objects$: Observable<NewObject[]> = this.objectSvc.getAllObjects("objects").pipe(
map(obj => obj.map(x => ({ name: x.name, title: alterString(x.title) }))),
shareReplay(1)
);

constructor(
private objectSvc: ObjectService
) { }

ngOnInit() {

somethingThatHappensToAdd = (response: any) => {
let data = JSON.parse(response);
data.forEach(x => {
let obj: NewObject = { name: x.name, title: alterString(x.title) }

// Here's where I'm trying to add the obj object into the already existing object array Observable
});
};

somethingThatHappensToDelete = (response: any) => {
let data = JSON.parse(response);
data.forEach(x => {
let obj: NewObject = { name: x.name, title: alterString(x.title) }

// Here's where I'm trying to delete the obj object from the already existing object array Observable
});
};

}
}

这是我的 example.component.html :
<div *ngFor="let o of objects$ | async">
<p>{{ o.name }}</p>
<p>{{ o.title}}</p>
</div>

这是我的服务 object.service.ts :
import { Injectable } from '@angular/core';
import { Observable, throwError, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { ClientApi, ApiException, NewObject } from '../client-api.service';

@Injectable({
providedIn: 'root'
})
export class ObjectService {

constructor(
private clientApi: ClientApi
) { }

getAllObjects(name: string): Observable<NewObject[]> {
return this.clientApi.getAllObjects(name)
.pipe(
map((x) => x.result),
catchError(err => {
if (ApiException.isApiException(err)) {
if (err.status === 404) {
return of<NewObject[]>(undefined);
}
}
return throwError(err);
})
);
}
}

JSON 中格式化响应后,我希望能够插入 obj对象进入 objects$ Observable 并让它反射(reflect)在 UI 上。

建议我使用 BehaviorSubject元素来实现这一点。任何人都可以就如何轻松做到这一点提出建议吗?

最佳答案

您可以创建一个 BehaviorSubject发出来自 somethingThatHappens() 的结果并使用 combineLatest()将结果与来自服务的结果合并。

objectsFromService$ = this.objectSvc.getAllObjects("objects").pipe(
map(obj => obj.map(x => ({ name: x.name, title: alterString(x.title) })))
);

resultsArray = []
resultsSubject = new BehaviorSubject(this.resultsArray);
results$ = this.resultsSubject.asObservable()

readonly objects$: Observable<NewObject[]> = combineLatest(
this.results$,
this.objectsFromService$
).pipe(
map(([results, objects]) => ([...results, ...objects])),
shareReplay(1)
)


somethingThatHappens = (response: any) => {
let data = JSON.parse(response);
data.forEach(x => {
let obj: NewObject = { name: x.name, title: alterString(x.title) }
this.resultsArray.push(obj)
});
this.resultsSubject.next(this.resultsArray)
};

我创建了一个有效的 stackblitz 项目: https://stackblitz.com/edit/angular-jnngwh

希望这能给你一个线索。

关于angular - 在 Angular/RXJS 中向对象数组的 Observable 添加/删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com