The p-dropdown component experiences issues with displaying the accurate label when the ngModel variable is an object

Currently, I am utilizing PrimeNG's p-dropdown component. Upon the initial page load, I am setting the ng-model, but the dropdown always displays the first element as the selected option.

HTML

<p-dropdown [options]="cities" [(ngModel)]="selectedCity"  optionLabel="name" [showClear]="true"></p-dropdown>
<p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>

TS

this.cities = [
            {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
            {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
            {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
            {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
            {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
        ];

Even if I modify the ng-model to a different city, the dropdown continues to display "New York" as the selected option.

Answer №1

Your issue could be related to the optionLabel="name" in your code, as it seems that your city object is missing a key called name.

To fix this, you can either update it to optionLabel="label" or modify your city object from

{label:'New York', value:{id:1, name: 'New York', code: 'NY'}}

to

{name:'New York', value:{id:1, name: 'New York', code: 'NY'}}

For further assistance, you can refer to this StackBlitz link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue: Typescript/React module does not have any exported components

I'm currently facing an issue with exporting prop types from one view component to another container component and using them as state type definitions: // ./Component.tsx export type Props { someProp: string; } export const Component = (props: ...

What is the best way to save a string for future use in Angular after receiving it from a POST request API?

I have been assigned to a project involving javascript/typescript/angular, even though I have limited experience with these technologies. As a result, please bear with me as I may lack some knowledge in this area. In the scenario where a user logs in, ther ...

Troubleshooting Angular and Auth0: Understanding the issue with loginWithRedirect() always returning isAuthenticated$ as false

I have previously posted this issue on the Auth0 Community Help Forum, but I am yet to receive a response despite posting it 2 weeks ago. Here is the link to my original post: Currently, my setup includes angular/cli 15.1.1 and auth0/auth0-angular 2.0.1. ...

How can you cycle through multi-dimensional arrays in Angular 2?

UPDATE#2 Unfortunately, I've been unable to successfully implement either of the suggestions provided. It's getting late, so I'm going to call it a night and revisit this tomorrow. A special thanks to Pradeep Jain for offering assistance. ...

Can property overloading be achieved?

Can functions be overloaded in the same way properties can? I'm interested in overloading properties to have separate documentation for different types passed to them. Currently, both values are set to the same value but I need distinct JSDoc for dif ...

What is the best way to pass a conditional true or false value to React boolean props using TypeScript?

I am currently utilizing the Material UI library in combination with React and Typescript. Whenever I attempt to pass a conditional boolean as the "button" prop of the component, I encounter a typescript error stating: Type 'boolean' is not assi ...

Utilize the pipe function to generate a personalized component

I have incorporated a custom pipe in my Angular 2 application to parse and make URLs clickable within messages displayed using an ngFor loop. If the URL links to a YouTube video, I also convert it into embed code. To optimize performance, I am looking to ...

How to toggle visibility of child elements in an HTML template using Angular 4+

I'm currently working on incorporating a basic "hover over a comment to display a reply button" feature in my angular application. Is it possible to create this effect using template reference variables exclusively? Something like this... <mat-l ...

Guide to automatically installing SSL certificates for Exceptions in your Browser

Apologies if this question may not appear valid, but I am currently facing an issue with an SSL certificate that I created locally and signed with a local CA. My frontend application is built with Angular, while my backend application is developed with Sp ...

Tips for generating a JSON-driven table in Angular 2

I'm attempting to build a dynamic datagrid in angular2 using a JSON object as the source. The challenge I face is not knowing the structure of the columns within the table, making it difficult to render the rows properly. My understanding is that I n ...

Is the ng-selector in Angular2 not sorting items alphabetically as expected?

This code snippet demonstrates the use of ng-selector in an .html file <ng-selector name="company" [(ngModel)]="company_selected" [formControl]="loanApplyForm.controls['company']" ...

Using jQuery in Jest test with Angular 5: A step-by-step guide

I am facing an issue with my Angular component that utilizes a CalendarService. Upon initialization of the component, I invoke the "calendarService.init()" method. The CalendarService is responsible for configuring a semantic-ui-calendar (which relies on ...

The current context for type 'this' cannot be assigned to the method's 'this' of type '...'

Currently, I am in the process of defining type definitions (.d.ts) for a JavaScript library. In this specific library, one of the methods accepts an object of functions as input, internally utilizes Function.prototype.bind on each function, and then expos ...

Is there a way to dynamically shift arrow key focus onto buttons in Angular using the left and right arrow keys?

I am facing an issue where pressing the arrow keys left and right does not focus or allow me to navigate through these buttons using the arrow keys. However, when checking the keycode values, they are printed according to the key pressed. I would like to k ...

Typescript: Shifting an image to the left and then returning it to the right

As a newcomer to Typescript, JavaScript, and front-end development, I am experimenting with creating a simulation of an AI opponent's "thinking" process when playing cards in a game. The idea is to visually represent the AI's decision-making by s ...

Exploring Architectural Designs in Angular2: Redux, Flux, Reactivity, RxJS, Ngrx, MVI, and More

Isn't it annoying how all these library and framework names start with R/N or sound so similar? react/redux | flux | ngrx | @ngrx/store | RxJS/ReactiveX | MVI | .... Can anyone make sense of this chaos? I'm trying to understand, please help me ...

Angular application featuring scrolling buttons

[Apologies for any language errors] I need to create a scrollable view with scroll buttons, similar to the image below: Specifications: If the list overflows, display right/left buttons. Hide the scroll buttons if there is no overflow. Disable the le ...

Generating formarray instances in Angular using data from MySQL records

I am currently working on a project where I need to populate formcontrols in formarray based on the data retrieved from a MySQL database. However, when I pass the success data from MySQL, I encounter an error that says "Cannot read property 'controls& ...

ngx-datatable detail row failing to expand properly

I am striving to develop an ngx-datatable component that can be utilized universally for consistent styling and functionality. Although most features are working correctly, I'm struggling to understand why the details row isn't expanding as expe ...

Having trouble setting the InnerHTML property of Null on my Ionic app, what could be the issue?

I'm working on a code to display the remaining time for generating a random code in the DOM. var count = setInterval(function () { var date = new Date(); var currentSecond = date.getSeconds(); ...