Angular:试图将我的功能变成RXJS
我的功能已实现并正常工作 我想在RXJS目录中使用过滤器将其rxjs置于rxjs目录中,并包含
并使用subscribe更新
posts: Post[] = [];
validPosts: Post[] = [];
authorName: string = "";
这是我的功能:
SearchPostsByName($event: Event) {
const searchKey = ($event.target as HTMLInputElement).value;
if (searchKey === '') {
this.validPosts = this.posts;
return;
}
this.validPosts = this.posts.filter(
value => value.authorName.includes(this.searchName)
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用输入字段构建搜索,请将输入元素绑定到
@viewchild
。在
AfterViewInit
中,您可以在元素的键
事件中添加一个可观察到的。通过管道
,您地图
事件到其值。您添加了debouncetime
,因此可观察到的发射不会太频繁,并且只能收听更改trough独特的nuntilChanged
。在订阅本身中,您可以根据输入来检查输入值并更改URL
queryparams
。在
oninit
中,您switchmap
可观察到的queryparams
到一个新观察的可观察到,该可观察到,该可观察到,该可观察到,通过服务从数据库中获取过滤后的帖子。在窗口上,刷新URL的过滤器查询也将设置为输入值。为了显示内容,您可以通过HTML中的
async
管道订阅可观察到的。State Management,使用RXJS | url vs本地客户端状态
To build a search using an input field, you bind the input element to a
@ViewChild
.In the
AfterViewInit
, you add an observable to thekeyup
event of the element. Through apipe
, youmap
the event to its value. You add adebounceTime
so the observable doesn't emit too often, and you only listen to changes troughdistinctUntilChanged
.Within the subscription itself, you check the input value and change the url
queryParams
based upon the input.During
OnInit
, youswitchMap
the observable of the route'squeryParams
to a new observable which will fetch the filtered posts from your database through the service. Upon a window refresh the filter query from the url will also be set as the input value.In order to show the content, you subscribe to the observable through an
async
pipe within the html.State management, using RxJS | URL vs local client state
StackBlitz | Filter through RxJS