我目前正在研究搜索栏组件。使用自定义管道,我可以显示项目的下拉列表。我需要将已过滤的项目列表(项目| 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]的字符串值传递到组件。任何人都可以建议解决方法吗?
答案 0 :(得分:1)
我可以为此提供解决方法。
示例: [这不是真实的代码]
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 ...