[英]Passing an autocomplete select value to parent component with primeNg and Angular 10
I have a component name update-data and a child component named show-select.我有一个名为 update-data 的组件和一个名为 show-select 的子组件。 In the parent HTML file I do add the selector and try to bind event from child component.在父 HTML 文件中,我添加了选择器并尝试从子组件绑定事件。
<app-show-select (categorySelected)="onCategorySelected($event)"></app-show-select>
In the UpdateDataComponent file I just make a basic console.log:在 UpdateDataComponent 文件中,我只创建了一个基本的 console.log:
onCategorySelected(categorySelected: CategoriesModel): void {
console.log('categorySelected: ', categorySelected);
}
My issue is coming from my child component: I do return a value from it but not the selected one.我的问题来自我的子组件:我确实从中返回了一个值,但没有返回选定的值。
export class ShowSelectComponent implements OnInit {
categories: CategoriesModel[];
filteredCategories: CategoriesModel[];
category: CategoriesModel;
@Output() categorySelected = new EventEmitter<CategoriesModel>();
categoryId: number;
constructor(private apiService: ApiService) { }
ngOnInit(): void {
this.getCategories();
}
private getCategories(): void {
const response = this.apiService.getCategories().subscribe(
(data) => {
this.categories = data as CategoriesModel[];
}
);
console.log('Get categories');
console.log('response ', response);
}
filterCategories(event): void {
this.filteredCategories = [];
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < this.categories.length; i++) {
this.category = this.categories[i];
if (this.category.categoryName.toLowerCase().indexOf(event.query.toLowerCase()) === 0) {
console.log(this.category.categoryName);
this.filteredCategories.push(this.category);
}
}
this.categoryId = this.category?.id;
this.categorySelected.emit(this.category);
}
}
The sub component HTML is inspired of the official documentation:子组件 HTML 的灵感来自官方文档:
<p-autoComplete
[(ngModel)]="category"
[suggestions]="filteredCategories"
field = "categoryName"
(completeMethod)="filterCategories($event)"
[size]="30"
[minLength]="1" placeholder="Hint: type a letter"
[dropdown]="true">
<ng-template let-category pTemplate="category">
<div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">
{{category.id}} {{category.categoryName}}
</div>
</ng-template>
</p-autoComplete>
My issue is that i pass the last entry of the categories list and not the one I selected.我的问题是我通过了类别列表的最后一个条目,而不是我选择的那个。 On the other hand, i do display the right content in this module when I add另一方面,当我添加时,我确实在这个模块中显示了正确的内容
<p> Category: {{category?.id||''}} {{category?.categoryName||'none'}} </p>
In the select component I display in the HTML page:在 select 组件中,我在 HTML 页面中显示:
Category: 2 Some value
In the console I display something totally different which match to the last entry of my json response and a choice that appears in the input when using Autocomplete with primeNg:在控制台中,我显示了完全不同的内容,这些内容与我的 json 响应的最后一个条目以及使用 PrimeNg 自动完成时出现在输入中的选项相匹配:
CategorySelected: {id: "56", categoryName: "OTHER CATEGORY", docDescription: "No description"}
I finally solved partially the most important part concerning my issue:我终于部分解决了关于我的问题的最重要部分:
I modified the p-autocomplete with a click event:我用点击事件修改了 p-autocomplete:
<p-autoComplete
[(ngModel)]="category"
...
(click)="getCategoryDetails($event)"
...>
<ng-template let-category pTemplate="category">
<div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5">
{{category.id}} {{category.categoryName}}
</div>
</ng-template>
</p-autoComplete>
I created this function:我创建了这个 function:
getCategoryDetails($event: MouseEvent): void {
this.categoryId = this.category?.id;
this.categorySelected.emit(this.category);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.