如何使用Angular通过Selectbox过滤表
给定一个自定义表组件以及自定义选择框组件,我希望能够通过从选择框中选择一个选项来过滤表行。
我的想法是首先将点击列表添加到我的选择框的选项中,因此,在单击选项时,将执行onoptionClicked()
方法。在此onoptionClicked()
方法中,我将发射options删除
事件:
@Output() optionSelected = new EventEmitter<string>();
onOptionClicked(option: string) {
this.optionSelected.emit(option);
}
我的表组件将在事件options opockelected
<div class="tbl" (optionSelected)="filterTableRows($event)">
<table>
<thead>
...
:但是,从根本上讲,这个想法是错误的,但是:
我的IDE显示出一个错误,说
发出。。事件选项未由任何适用指令或Div Element
。该表在选项置换事件上没有反应。
似乎问题是我的表组件和我的Select-box组件是不同的组件,显然它们无法通过事件发射器进行通信。有人可以帮我吗?
Given a custom table component as well as a custom select box component, I want to be able to filter the table rows by selecting an option from the select box.
My idea was to first add a click-listener to the options of my select box, so when clicking on an option, the onOptionClicked()
method will be executed. Inside this onOptionClicked()
method, I will emit an optionSelected
event:
@Output() optionSelected = new EventEmitter<string>();
onOptionClicked(option: string) {
this.optionSelected.emit(option);
}
My table component will react upon the event optionSelected
:
<div class="tbl" (optionSelected)="filterTableRows($event)">
<table>
<thead>
...
There seems to be something fundamentally wrong with this idea, however:
My IDE shows an error saying
Event optionSelected is not emitted by any applicable directives nor by div element
.The table does not react on the optionSelected event.
It seems the issue is that my table component and my select-box component are different components, apparently they cannot communicate via event emitters. Can someone help me out, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为使用
@output()
decorator < /a>。这是使用此组件来向其他组件发出信号的东西。您似乎在同一组件中使用它。而是使用a replasesUbject 保留您的过滤器:
要进行数据过滤,您可以订阅您到重播主题:
注意:请记住,当组件被销毁时,请记住完成任何订阅,以避免常见内存泄漏。
I think there is a misunderstanding on the use of the
@Output()
decorator. This is something to be used to emit signals to other components using this one. You seem to be using it in the same component.Instead, use a ReplaySubject to retain your filter:
To do the data filtering you could subscribe to the replay subject:
Note: please remember to complete any subscriptions when the component is destroyed to avoid the common memory leak.