[英]Angular 2 display newly added item to http in other component
This is probably easy for someone, but I just can't get it.这对某人来说可能很容易,但我就是无法理解。 So I have a list of items to display:
所以我有一个要显示的项目列表:
My service that fetches the data, configService.ts我的获取数据的服务 configService.ts
ngOnInit() {
getConfig(): Observable<any> {
return this.http.get<any>('/api/config').map(res => res);
}
}
My savedSearhes component that populates data in the ngFor:我在 ngFor 中填充数据的 savedSearhes 组件:
this.configService.getConfig().subscribe(data => {
this.userSavedSearches = data.isr.me.savedSearches //get an array of items
});
the html to display data:显示数据的html:
<div *ngFor="let savedsearch of userSavedSearches">
{{savedsearch.name }}
</div>
The main issue I have, is I have another component that I use to add a new item to the same server.我遇到的主要问题是我有另一个组件可用于将新项目添加到同一台服务器。
saveSearch() {
this.saveSearchObject = {
name: this.SearchName,
description: this.SearchDescription,
context: this.theContext,
}
this.searchService.createSavedSearch(this.saveSearchObject).subscribe(data => {
console.log(data) // newly added item to server
})
}
The service that posting new item to server:将新项目发布到服务器的服务:
createSavedSearch(search: SavedSearch): Observable<SavedSearch> {
return this.http.post<SavedSearch>('/api/searches/', search)
}
When I add a new item, the item actually gets added to the server.当我添加一个新项目时,该项目实际上被添加到服务器中。 But I don't see the "savedSearches" component display added item, only when I reload the page I can see new item added.
但是我没有看到“savedSearches”组件显示添加的项目,只有当我重新加载页面时才能看到添加的新项目。 How to add new item to the server and see its being added with new item in other component without reloading the page.
如何在不重新加载页面的情况下将新项目添加到服务器并查看它是否与其他组件中的新项目一起添加。
You can achieve it by Creating a subject where saveSearch function lies
let subjectUserSavedSearches = new Subject();
let obsrvUserSavedSearches = subjectUserSavedSearches.AsObservable();
saveSearch() {
this.saveSearchObject = {
name: this.SearchName,
description: this.SearchDescription,
context: this.theContext,
}
this.searchService.createSavedSearch(this.saveSearchObject).subscribe(data => {
this.userSavedSearches = data;
this.subjectUserSavedSearches.next(this.userSavedSearches);
})
}
Now watch that obsrvUserSavedSearches on the component you need to show data.
The best way is to move methods getConfig() and saveSearch() in a service and just create a subject for userSavedSearches and an Observable to watch the same.最好的方法是在服务中移动 getConfig() 和 saveSearch() 方法,然后为 userSavedSearches 和 Observable 创建一个主题来观看相同的内容。
You won't see it unless you do a getConfig()
again.除非您再次执行
getConfig()
,否则您将看不到它。 Use a .switchMap()
to chain your http calls.使用
.switchMap()
链接您的 http 调用。
saveSearch() {
this.saveSearchObject = {
name: this.SearchName,
description: this.SearchDescription,
context: this.theContext,
}
this.searchService.createSavedSearch(this.saveSearchObject)
.switchMap(data => {
console.log(data) // newly added item to server
return this.configService.getConfig();
})
.subscribe(data => {
this.userSavedSearches = data.isr.me.savedSearches //get an array of items
})
}
Otherwise, unless your savedSearches()
actually returned a newly refreshed list, you can do it in your subscribe:否则,除非您的
savedSearches()
实际上返回了一个新刷新的列表,否则您可以在订阅中执行此操作:
saveSearch() {
this.saveSearchObject = {
name: this.SearchName,
description: this.SearchDescription,
context: this.theContext,
}
this.searchService.createSavedSearch(this.saveSearchObject).subscribe(data => {
this.userSavedSearches = data
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.