0

I want to return the result of a function in my component into a dropdown options:

this is my template:

<th>
  <p-dropdown
   [options]="getNames()"
   [required]="true"
   optionLabel="label"
   (onChange)="filterName($event.target.value)"
 >
</p-dropdown>

this is my function

    public getNames() {
    let namesLabels: any[];
    this.backendService.getNames().subscribe(response => {
    this.namesSelect = response.filter(item => item.name !== '');
    this.namesSelect .forEach(prod => namesLabels.push(prod.name));
    });
    return namesLabels;
}

the array namesLabels is well charged, means when I add a console.info for each element of namesLabels I found my wanted result

in the screen I don't found any value in the dropdown, any help please ?

1 Answer 1

0

I guess you need something as below:

// .ts
options: any[];

ngOnInit() {
  this.getNames();
}

private getNames() {      
  this.backendService.getNames().subscribe(response => {
    this.namesSelect = response.filter(item => item.name !== '');
    this.namesSelect.forEach(prod => this.options.push(prod.name));
  });
  
}

//.html
<p-dropdown
   [options]="options"
   [required]="true"
   optionLabel="label"
   (onChange)="filterName($event.target.value)"
 >
</p-dropdown>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you Evgency, I have an error TypeError: namesLabels is undefined
I have updated my answer, see getNames function definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.