Angular-从可观察的
考虑一个表组件,该表组件保存变量rows =雇员[]
。这些行是由我当前正在尝试实施的服务设置的。我面临的挑战是,我希望能够在不重新加载所有数据的情况下将行添加和删除在桌子上。
我已经问一个类似的问题并获得了一些惊人的帮助,这使我掌握了以下中间解决方案:
private employeeAdded$ = new Subject<Employee>();
private serverEmployees$ = this.httpClient.get<Employee[]>(this.EMPLOYEES_URL);
employees$ = merge(
serverEmployees$,
employeeAdded$.pipe(
map(x => [x])
)
).pipe(
scan((acc, cur) => acc.concat(cur), [])
);
只是要清楚:此代码允许初始化雇员$
可观察到雇员[]
数组,还向其添加了另一个员工,而无需向我的服务器发送另一个get请求以检索坐在那里的所有员工。我想避免每次添加或删除员工时将GET请求发送到服务器。
但是,问题是:我不知道如何从员工$
中删除员工。
我当时在想:我可能必须添加另一个可观察的homployeedeedreated $ = new主题&gt;()
,并以某种方式将其集成到Merge
调用中。当前,我的后端只是在发送删除请求时返回已删除员工的ID。如果我将整个员工列表返回而无需删除的员工列表,我可以将其集成到我的Merge
调用中。但是,这正是我要避免的:每次删除员工时都必须重新加载整个数据库。
无论如何:我没有解决这个问题的解决方案。我很感兴趣,如果可以以类似的方式添加员工,或者需要更多的努力,在这种情况下,我可能会派遣另一个重新要求。
Consider a table component, which holds the variable rows = Employees[]
. The rows are set by a service, which I am currently trying to implement. The challenge that I am facing is that I want to be able to both add and remove rows to my table without reloading all my data afterwards.
I have asked a similar question before and received some amazing help, which has led me to the following intermediate solution:
private employeeAdded$ = new Subject<Employee>();
private serverEmployees$ = this.httpClient.get<Employee[]>(this.EMPLOYEES_URL);
employees$ = merge(
serverEmployees$,
employeeAdded$.pipe(
map(x => [x])
)
).pipe(
scan((acc, cur) => acc.concat(cur), [])
);
Just to be clear: This code allows for initializing the employees$
observable with an Employee[]
array and also adding another employee to it without having to send another GET request to my server to retrieve all employees sitting there. I want to avoid sending a GET request to my server every time I add or remove an employee.
The problem, however, is: I don't know how to remove an employee from employees$
.
I was thinking: I will probably have to add another observable employeeDeleted$ = new Subject<number>()
and somehow integrate this within the merge
call. Currently, my backend simply returns the id of the deleted employee upon sending a DELETE request. If I was to return the entire list of employees without the deleted one instead, I could integrate this into my merge
call. This, however, is exactly what I am trying to avoid: Having to reload the entire database every time I delete an employee.
Anyways: I don't have a solution for this problem. I am interested if this can be done in a similar fashion as adding an employee, or if it requires substantially more effort, in which case I might go with sending another GET-request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论