Angular2/4/6 将自定义管道过滤列表从 HTML 传递到组件
Angular2/4/6 Pass Custom Pipe Filtered List from HTML to Component
我目前正在开发搜索栏组件。使用自定义管道,我能够显示项目的下拉列表。我需要将过滤后的项目列表 (Items | 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;
}
}
}
但是,我无法正确检索对象列表(内容变量)。它作为 [object Object] 的字符串值从 html 传递到组件。任何人都可以建议解决方法吗?
我可以为此提供解决方法。
- 使用普通实用函数并在该函数中实现过滤逻辑。
- 在按键事件中从 searchbar.component.ts 调用该函数并填充
过滤后的内容
- 然后根据需要在searchbar.component.ts中使用过滤后的内容
- 也在您的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 ...
我目前正在开发搜索栏组件。使用自定义管道,我能够显示项目的下拉列表。我需要将过滤后的项目列表 (Items | 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;
}
}
}
但是,我无法正确检索对象列表(内容变量)。它作为 [object Object] 的字符串值从 html 传递到组件。任何人都可以建议解决方法吗?
我可以为此提供解决方法。
- 使用普通实用函数并在该函数中实现过滤逻辑。
- 在按键事件中从 searchbar.component.ts 调用该函数并填充 过滤后的内容
- 然后根据需要在searchbar.component.ts中使用过滤后的内容
- 也在您的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 ...