A step-by-step guide on assigning values to an Angular Material Auto Complete component in Angular 7

Hey there! I'm currently using the Angular Material Auto complete component in my Angular 7 app and I'm trying to find a way to bind a value from an API response to it. Can someone help me out with a solution for this?

HTML:

<mat-form-field>
    <input type="text" [(ngModel)]="templateId" name="templateId" #templateId="ngModel" placeholder="Template" matInput
        [matAutocomplete]="auto" aria-label="Number" (keyup)="filter($event)">
    <mat-autocomplete #auto="matAutocomplete" (optionSelected)="onTemplateChange($event)">
        <mat-option *ngFor="let temp of filtered" [value]="temp">
            {{temp.name}}
        </mat-option>
    </mat-autocomplete>
</mat-form-field>

Ts:

onTripTemplateChange($event) {
        this.templateId = $event.temp.name;
}
  1. I want the value to be selected by default when I open the component
  2. When I change the option from the auto complete list, I need to update the selected value

If you have a solution, please share it with me!

Answer №1

According to the original documentation on angular material autocomplete, it is necessary to set the value for the autocomplete by also setting the value for the input element.

You can find the complete documentation here:

An example of this functionality can be seen in the sample app available on stackblitz: https://stackblitz.com/angular/egaebogmkaa?file=src%2Fapp%2Fautocomplete-simple-example.ts

The provided sample app demonstrates a basic autocomplete scenario, where setting a value for the input in the .ts file will automatically select it. For instance:

 myControl = new FormControl('One');

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

Steps to access email template through an Excel web add-in

Currently, I am developing a new addin that aims to extract data from Excel and insert it into a word document. The final step would be attaching this document to an email in Outlook. While I have managed to achieve this using Power Automate, I prefer to ...

To properly display a URL on a webpage using Angular, it is necessary to decode

After my console.log in Angular 5 service call to the component, I can see the correct data URL being displayed http://localhost:4200/inquiry?UserID=645 However, when it is inside an Angular for loop in the HTML template, it displays http://localhost:42 ...

Click to Rotate Angular Chevron

Is it possible to animate the rotation of a chevron icon from left-facing to right-facing using Angular? CSS: .rotate-chevron { transition: .1s linear; } HTML: <button [class.button-open]="!slideOpen" [class.button-close]="slideOpe ...

Mastering Angular Service Calls in TypeScript: A Comprehensive Guide

In the midst of my TypeScript angular project, I am aiming to revamp it by incorporating services. However, I have encountered an issue where when calling a function within the service, the runtime does not recognize it as the service class but rather the ...

What is the process for adding an element using Angular Material2 design?

I am in the process of creating a template where, upon clicking a button, I want to append specific elements. While I have successfully appended the elements using the code below, I am facing challenges with adding styles and integrating angular-material2 ...

Passing HTML content to an ng-bootstrap modal in Angular 2+

My modal setup in the Component Library looks like this. Keep in mind, I am working within a Component Library, not just the application. Within my Component Library... The template is as follows: <div class="modal-header"> <h4 class="mt- ...

Javascript operations for duplicating and altering arrays

In my Angular application, I am working with an array called subAgencies that is connected to a datasource. I need to implement 2-way binding on this array. Currently, I have a method in place where I copy the contents of the original array to a new one, ...

Angular 2: Toggle multiple checkboxes with a single click

I am faced with a scenario where I have 5 checkboxes implemented as shown below <input type="checkbox" [checked]="chk1" (change)="chk1 = !chk1" />chk1 &nbsp; <input type="checkbox" [checked]="chk2" (change)="chk2 = !chk2" />chk2 &nbsp; ...

Invoke the custom form validator multiple times

My approach involves using a specialized validator to ensure that the values of two input fields are in sync: import { FormGroup } from '@angular/forms'; // custom validator to check that two fields match export function MustMatch(controlName: ...

How to toggle CSS class in Angular2/Typescript to mimic radio buttons behavior

Is there a way to create a radio button group using UL and LI elements in Angular2 and Typescript? The goal is to have all the anchors function like a radio button group where only one can be selected at a time. The selected anchor should remain "clicked" ...

Display a customized modal showcasing the data of a particular user

Seeking advice on how to pass data to a modal based on a specific user. I have an array with user information and would like to display their name and email in a modal when the user is clicked on. Any suggestions on how to accomplish this? ...

Utilizing Angular's ngx-bootstrap date range picker for a customized date range filtering system

Currently, I am incorporating ngx-bootstrap's datepicker functionality and utilizing the date range picker. This feature allows users to choose a start and end date. After selecting these dates, my goal is to filter the array(content) based on whethe ...

Transforming Java Web Project into Angular using Java

I'm currently working on a Java web project that uses JSP for the frontend and Java for the backend. I'm looking to convert this project to have an Angular frontend and keep the Java backend. Despite my efforts in searching online, I haven't ...

Modifications made to Angular 7 templates are not appearing in MVC 5

Currently, I am utilizing Angular 7 with MVC5 in Visual Studio 2019. However, I have encountered an issue while trying to render the Angular template in my index.cshtml file. I access the Angular template in index.cshtml using ``. The template loads perfec ...

What is the significance of parentheses when used in a type definition?

The index.d.ts file in React contains an interface definition that includes the following code snippet. Can you explain the significance of the third line shown below? (props: P & { children?: ReactNode }, context?: any): ReactElement<any> | nu ...

Angular - Issue with Function Observable<number> in Development

Currently, I'm working on writing TypeScript code for a component. Within this TypeScript class, I have created a function that is meant to return a number representing the length of an array. My goal is to have this function work like an Observable. ...

Combine the information from 3 separate subscriptions into a single object using RxJS in Angular 9

I am seeking assistance with merging data from 3 different sensors into one object. I am using Cordova plugins to retrieve accelerometer, gyroscope, and magnetometer data. However, I am facing an issue in subscribing to all three observables simultaneously ...

Understanding the differences between paths and parameters of routes in express.js

My express application has the following routes: // Get category by id innerRouter.get('/:id', categoriesController.getById) // Get all categories along with their subcategories innerRouter.get('/withSubcategories', categoriesControll ...

Different ways to reference a variable in Typescript without relying on the keyword "this" throughout the codebase

Can we eliminate the need to write "this" repeatedly, and find a way to write heroes, myHero, lastone without using "this"? Similar to how it is done in common JavaScript. https://i.stack.imgur.com/TZ4sM.png ...

Modify the name of the document

I have a piece of code that retrieves a file from the clipboard and I need to change its name if it is named image.png. Below is the code snippet where I attempt to achieve this: @HostListener('paste', ['$event']) onPaste(e: ClipboardE ...