Issue with Mat Autocomplete not populating input value when utilized with a formControl

I've encountered an issue with my custom autocomplete component that implements ControlValueAccessor. I'm attempting to set the value from the parent component using

form.get('productName').setValue('Product 1');
. While this successfully sets the value in the form, it doesn't reflect in the associated input field for the autocomplete.

In a stackblitz example, I demonstrate the issue where the input fails to display the assigned value. Interestingly, removing the attribute

[matAutocomplete]="auto"
from the input immediately resolves the visibility problem in the UI.

You can view the example here: https://stackblitz.com/edit/angular-autocomplete-form-control?file=src%2Fapp%2Fapp.component.ts

I also attempted the solution mentioned in this thread (link provided), but unfortunately, it didn't yield any success -

Answer №1

When setting the initial value, it may not show up in the input field because the displayFn transformer expects the value to be an object with a `name` property, but you have assigned a string.

Since you are assigning the value of `mat-autocompelte` as `product.name`, there is no need to use the `displayFn` function at all.

Now that you are able to populate the input text, make sure to complete your ControlValueAccessor implementation.

You should utilize the dedicated `onChange` method to update the host control value.

This means you must call the `onChange` method whenever you want to update the host control value. For mat-autocomplete, you can listen for `optionSelected` events:

<mat-autocomplete ... (optionSelected)="onChange($event.option.value)">

Modified Stackblitz

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

Error message TS2339: The method 'findAll' is not defined in the current context

Despite following the Sequelize guide for TypeScript configuration, I am unable to resolve an issue with my database connection. The connection is active, but I am struggling with a specific type problem: TSError: ⨯ Unable to compile TypeScript: controll ...

How to make a node application run as an executable within an NX workspace

The structure of an NX workspace really caught my attention, which led me to start using it for a new CLI project I was working on. I began by setting up a @nrwl/node:application, but I'm currently facing some issues trying to make it executable. I ...

Getting an error message like "npm ERR! code ENOTFOUND" when trying to install Angular CLI using the command "

Currently, I am eager to learn Angular and have already installed Node version 18.13.0. However, when attempting to install Angular CLI using the command npm install -g @angular/cli, I encountered an issue: npm ERR! code ENOTFOUND' 'npm ERR! sys ...

Upgrading from Angular 4 to 9

What is the recommended approach for migrating from Angular 4 to 9 - should one go directly from 4 to 9, or is it better to first upgrade to version 7 and then to 9? ...

Observable function encountering difficulties returning nested values

I've been working on a function that returns an observable. test(id: int): Observable<Group>{ this.http.get('test/').subscribe( (result:any) => { resultingVal = Group.fromJson(result.group); }); } Right now, the function ...

Can models drive reactive forms by automatically mapping them to FormGroups?

Is it possible to automatically generate a FormGroup from a Model? If I have a Model with multiple Properties: Model: Person firstName: string, lastName: string, street: string, country: string .... And I would like to create a basic FormGroup based on ...

Toastr service experiencing issues on Internet Explorer browser

While implementing toastr notifications in my Angular application, I encountered an issue with compatibility across browsers. Specifically, the ngx-toastr module functions properly in Chrome and Firefox, but not in Internet Explorer. Interestingly, it wo ...

Using Typescript and JSX to render a component that has been passed as an argument

I am seeking to create a function that will render a React component passed as an argument. I aim to accommodate both Component and StatelessComponent types with the following approach: function renderComponent(component: React.ComponentClass<any> | ...

Is there a way to create a new perspective for Ion-Popover?

Looking for a solution: <ion-grid *ngIf="headerService.showSearch()"> <ion-row id="searchbar" class="main-searchbar ion-align-items-center"> <ion-col size="11"> ...

How to verify user authorization in Angular and PHP

After setting up authentication with Angular and PHP, I noticed that when the page is reloaded, users are required to re-enter their login credentials. This happens because upon reloading, Angular's "IsLoggedIn" variable gets reset to false. Is there ...

"Setting up a schema in TypeORM when connecting to an Oracle database: A step-by-step guide

As a newcomer to TypeORM, I'm using Oracle DB with Node.js in the backend. Successfully connecting the database with TypeORM using createConnection(), but struggling to specify the schema during connection creation. One workaround is adding the schem ...

Angular 2: Retrieving the Currently Active Tab from a Bootstrap Tab Using TypeScript

Using a basic bootstrap tab, I am looking to identify the active tab in order to perform a function in my .ts file. Below is the code snippet: <li role="presentation" class="active"> <a href="#daily" aria-controls="daily" role="tab" data-toggl ...

Show content based on dynamically selected dropdown in Angular

I am facing an issue with a dynamic select element in Angular. I am struggling to display the selected values on the view because I cannot seem to create a click event on the dropdown options or access the selected option via a click event on the <selec ...

Disabling the use of console.log() in a live environment

In an effort to disable console logs for production environments in my angular application, I implemented the code below. While it successfully suppresses logs in Chrome, IE 11 continues to display them. Here is the snippet from main.ts: if (environment. ...

How can errors be captured when working with chained rxjs observables in an combineLatest scenario?

After reading through this thread, I encountered the following scenario: Observable.combineLatest( this.translate.get("key1"), this.translate.get(""), this.translate.get("key3"), this.translate.get("key4") ) .subscr ...

What are the benefits of dealing with various versions of Angular?

Currently, I am using Angular version 8.2.14 which was installed globally by running the command npm install -g @angular/cli. All my projects are also built on this same version of Angular. With the recent release of Angular version 9, I am contemplating ...

Angular Update Component on Input ChangeEnsuring that the component is automatically

<div class=" card-body"> <div class="row"> <div class=" font-icon-list col-lg-2 col-md-3 col-sm-4 col-xs-6 col-xs-6" routerLinkActive="active" *ngFor="let subject of subjects"> <div class=" fon ...

What's the deal with Angular query parameters and parentheses?

My Angular application has a routing structure that includes a query parameter. Here is an example: const routes: Routes = [{ path: '', component: DefaultComponent }, { path: ':uname', component: ProductDisplayComponent }]; Whe ...

Implement the click event binding using classes in Angular 2

If I have the template below, how can I use TypeScript to bind a click event by class? My goal is to retrieve attributes of the clicked element. <ul> <li id="1" class="selectModal">First</li> <li id="2" class="selectModal">Seco ...

Struggling to remove CLI from my Mac operating system

I seem to be stuck on CLI version 8.3.21 while using MacOS Big Sur. I'm not currently in a project folder and have deleted all node_modules directories from my existing Angular projects. To try and remove it, I've attempted the following commands ...