I have a list of JSON object, 1 of the sample is below
[
{
id: 'cont-609',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
}]
My goal here is to access emailAddress (contactMedium[0].characteristic.emailAddress) inside PrimeNG autoComplete attribute 'field' so that I can display list of emails in drop down.
There will be always 1 element inside contactMedium
Below is my typescript code
import { Component, ViewChild } from '@angular/core';
import { MenuItem } from 'primeng/api';
import { SelectItem } from 'primeng/api';
import { SelectItemGroup } from 'primeng/api';
import { FilterService } from 'primeng/api';
import { AutoComplete } from 'primeng/autocomplete';
import { CountryService } from './countryservice';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [CountryService, FilterService]
})
export class AppComponent {
userDetails: any[];
selectedUserDetails: any[];
selectedValue: any;
selectedUserDetail: any;
constructor() {}
ngOnInit() {
this.userDetails = [
{
id: 'cont-609',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
},
{
id: 'cont-610',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
},
{
id: 'cont-611',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
},
{
id: 'cont-612',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
},
{
id: 'cont-614',
contactMedium: [
{
characteristic: {
emailAddress: '[email protected]'
}
}
]
}
];
}
filterUserDetails(event) {
let filtered: any[] = [];
for (let val of this.userDetails) {
filtered.push(val);
}
this.selectedUserDetails = filtered;
}
getUserDetails(): Promise<any[]> {
return Promise.resolve(this.userDetails);
}
chooseItem(event) {
this.selectedUserDetail =
event.contactMedium[0].characteristic.emailAddress;
}
}
Below is my HTML code
<h5>Dropdown Testing</h5>
<p>selectedUserDetail : {{selectedUserDetail}}</p>
<p>TestVal : {{testVal}}</p>
<p-autoComplete [(ngModel)]="selectedUserDetail" [suggestions]="selectedUserDetails"
(completeMethod)="filterUserDetails($event)" [dropdown]="true" field="contactMedium">
<!--<ng-template let-userDetails pTemplate=" item">
<div>{{userDetails.contactMedium[0].characteristic.emailAddress}}</div>
</ng-template> -->
</p-autoComplete>
This part of the attribute is not working, field="contactMedium[0].characteristic.emailAddress" but if I place "id" from the json which is not array it works field="id" , but the goal here is to display emails.
Below is the link of the code if you want to access for experiment :
https://stackblitz.com/edit/primeng-autocomplete-demo-dyihrs?file=src%2Fapp%2Fapp.component.html