Angular2 / 4/6将自定义管道过滤列表从HTML传递到组件

iBlehhz

我目前正在研究搜索栏组件。使用自定义管道,我可以显示项目的下拉列表。我需要将已过滤的项目列表(项| CustomPipe:search_input)从searchbar.component.html传递到searchbar.component.ts,但我不确定如何

searchbar.component.html

<ul *ngIf="(Items | CustomPipe : search_input).length>0" class="list-group dropdown-container">
    <li *ngFor="let item of Items | CustomPipe : search_input; index as i" [class.active]="i == arrowkeyLocation" (mouseover)=changeStyle($event)
        (mouseleave)=changeStyle($event) (click)="showConfirm(item)" class="list-group-item" [innerHTML]="item.model | highlight : search_input"></li>
</ul>

我目前的做法:

<input #filterSize type="hidden" value="{{(Items | CustomPipe : search_input).length}}">
<input #filterContent type="hidden" value="{{(Items | CustomPipe : search_input)}}">

searchbar.component.ts

export class SearchbarComponent implements OnInit {
 arrowkeyLocation: number = 0;
@ViewChild('filterSize') filterSize: any;
@ViewChild('filterContent') filterContent: any;
@Output() onSelect: EventEmitter<number> = new EventEmitter<number>();
constructor() {
}
ngOnInit() { }
changeStyle(event) {

    let content = this.filterContent.nativeElement.value;
    let dropdownSize = this.filterSize.nativeElement.value;

    if (event.type == "keydown") {
        switch (event.keyCode) {
            case 38: // this is the ascii of arrow up
                if (this.arrowkeyLocation == -1) {
                    this.arrowkeyLocation = dropdownSize;
                }
                this.arrowkeyLocation--;
                break;
            case 40: // this is the ascii of arrow down
                if (this.arrowkeyLocation == dropdownSize) {
                    this.arrowkeyLocation = -1;
                }
                this.arrowkeyLocation++;
                break;
            case 13:
                this.onSelect.emit(content[this.arrowkeyLocation]);
                break;
        }
    }
}

但是,我无法正确检索对象列表(内容变量)。它从html作为[object Object]的字符串值传递到组件。任何人都可以建议解决方法吗?

马林杜·桑达鲁万

我可以为此提供一种解决方法。

  1. 使用普通的实用程序功能,并在该功能中实现过滤器逻辑。
  2. 在按键事件上从searchbar.component.ts调用该函数并填充过滤的内容
  3. 然后根据需要searchbar.component.ts中使用过滤后的内容
  4. 还可以searchbar.component.html中使用过滤的内容

示例: [这不是真实的代码]

searchbar.component.ts

KeyPressCallback(search_input) {
    this.filteredContent = utilityFunctionToFilter(Items, search_input);
    // Do whatever you like with filteredContent.

}

searchbar.component.html

 <ul *ngIf="filteredContent.length>0" class="lis ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章