"Despite the inability to use the clear input field function in Angular, users can still effectively clear

Why does the X button not clear and refresh the field when clicked, but using backspace does? I want it to clear when the X button is clicked.

<mat-form-field appearance="standard" fxFill>
            <mat-label style="font-size: 12px;">Filter users by name, company or title</mat-label>
            <input matInput #searchTransactionUserInput placeholder="" (keyup)="applyFilter($event)">
            <button mat-button *ngIf="searchTransactionUserInput" matSuffix mat-icon-button aria-label="Clear"
                (click)="clearSearch()">
                <mat-icon>close</mat-icon>
            </button>
        </mat-form-field>

#typescript code

clearSearch() {
    console.log("clear")
    this.searchTransactionUserInput.nativeElement.value = '';
    this._transactionUserPageEvent();
}

https://i.sstatic.net/voug4.png

Answer №1

If ngModel is acceptable in your situation, you can implement the following approach:

 <mat-form-field appearance="standard" fxFill>
                <mat-label style="font-size: 12px;">Filter users by name, company or title</mat-label>
                <input matInput [ngModel]="searchTransactionUserInput" placeholder="" (keyup)="applyFilter($event)">
                <button mat-button *ngIf="searchTransactionUserInput" matSuffix mat-icon-button aria-label="Clear"
                    (click)="clearSearch()">
                    <mat-icon>close</mat-icon>
                </button>
            </mat-form-field>

In the *.component.ts file:

searchTransactionUserInput = '';//string variable used for ngModel instead of ViewChild and ElementRef

clearSearch(): void {
    this.searchTransactionUserInput = '';
    this._transactionUserPageEvent();
}

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

An easy guide to displaying an HTML string in a DIV with Angular 6

I have a Angular 6 application that interacts with the python API. The API responds with HTML data that I want to display on my existing page within a specific div element. I have attempted various methods but have not been successful. Test.ts public myT ...

Breaking up React code within the React.createElement() function

I am encountering an issue with lazily loaded pages or components that need to be rendered after the main page loads. When using createElement(), I receive the following error: LazyExoticComponent | LazyExoticComponent is not assignable to parameter of ty ...

Is it possible to utilize router-outlet within a component in a shared module?

My goal is to create a consistent user experience across all my feature modules by including a common sidebar in each of them. Currently, I am utilizing Bootstrap5 grids and a router-outlet within some grid elements to maintain a standardized look and feel ...

How can I iterate through a variable in TypeScript?

initialize() { var elements = []; for (let i = 1; i <= 4; i++) { let node = { name: `Node ${i}` }; elements.push({ [`node${i}`]: node }); if (i < 4) { let edge = { source: `node${i}`, target: ...

When the page is refreshed, Vercel's Next.JS success/error pattern is thrown due to the "window is not defined" error

Currently, I am working on enhancing a Next.js website that is hosted on Vercel. Upon deploying the page, I encountered the following error initially: GET] / 17:53:00:19 2022-09-12T14:53:00.262Z 938c1a2e-ce7c-4f31-8ad6-2177814cb023 ERROR Uncau ...

Creating smooth transitions between different components in Ionic 2 through animations

I have two components in my application. I am looking to create an animated transition between these pages. Specifically, I want to implement a flipping effect where page 1 transitions into page 2. Is there a plugin available for achieving this in Ionic ...

The parameter 'X' cannot be assigned to the argument of type 'X'

Hello there! I'm diving into Type Script for the first time with VSCode. Encountering these errors: error TS2322: Type '() => string' is not assignable to type 'string'. error TS2322: Type '() => number' is ...

The Vitest test is not compatible with PrimeVue3 Dialogs and does not function as intended

I am currently working on a project that involves using PrimeVue components, and the time has come to conduct some tests. Below is the code for the test: import { beforeEach, describe, expect, it } from 'vitest' import type { VueWrapper } from & ...

Unraveling the Intricacies of Manipulating Values through mockStore.overrideSelector

There is a certain selector that I'm working with, and it returns either true or false. To ensure accurate testing in my unit test, I need to cover scenarios where this value is both true and false. To set up for the test, I have initially configured ...

The upcoming developer manages to execute the program successfully, however, it continues to load indefinitely

Executing the command yarn dev consistently runs successfully in my VS Code terminal: $ yarn dev yarn run v1.22.19 warning ..\..\..\..\package.json: No license field $ next dev ready - started server on 0.0.0.0:3000, url: http://localho ...

When logging off from an Angular2 application, the OIDC-client does not properly clear the cookies for the MVC application

I currently have an authorization server that is being used by both my Angular2 app and MVC webapp. In my Angular2 app, I've implemented authorization using the oidc-client JavaScript package. Everything works well except for the logout functionality ...

The TypeScript error "File 'XXX' is not recognized as a module" is preventing successful compilation

Is there a way to import a module from an external file into another TS file? Even after following the steps, when I tried to do so in VSCode, it gave me an error saying that the file 'XXX' is not a module: Here's the error I encountered I ...

next-intl failing to identify the primary language setting

When testing next-intl for the app directory in the Next.js v13.4.0, I encountered an issue where the default locale was not recognized. Despite following the documentation step by step, I also faced significant challenges with the client-side version in p ...

Exploring the intricacies of pattern matching with JavaScript visualization

I am currently working on improving my pattern matching function by creating a visualizer for it. To achieve this, I need to slow down the execution of each comparison. My approach involves declaring the i and j variables outside of the function so that I ...

"Utilize Vue's model binding feature to assign a numerical value

I've encountered an issue while using v-model on an input box. I'm trying to bind the value as a number, but it doesn't seem to be working as expected. When I perform an operation like myModel += 25, instead of getting 125, I end up with 100 ...

Retrieve the child nodes from the array and organize them into a

Given an array of regions, where the highest region has key: 10 and parent_id: null, the goal is to restructure this array in order to return a tree representation. The desired regions tree structure for input [10] should be: Egypt Zone 1 Tagamo3 Giza H ...

Troubleshooting: Angular 2 View not reflecting changes after array push

I have encountered an issue with my two child components. They are both meant to share data from a json file that I load using the http.get/subscribe method. Oddly enough, when I try to push new data into the array, it doesn't seem to update in the vi ...

having trouble integrating and utilizing TypeScript with socket.io

Trying to utilize the npm typescript @types/socket.io definition. To set it up, I followed these steps: npm install --save @types/socket.io npm install --save socket.io After that, my package.json now looks like this: ... "devDependencies": { ...

Is it possible for me to exclude generic parameters when they can be inferred from another source?

Imagine having a scenario like this: type RecordsObject<T, K extends keyof T> = { primaryKey: K; data: Array<T>; } where the type K is always derived from the type T. Oftentimes, when I try to declare something as of type RecordsObject, ...

Utilizing FileInterceptor with a websocket in NestJs: A Step-by-Step Guide

Is it possible to implement this on a websocket, and if so, how can I achieve that? @UtilizeInterceptors( DocumentInterceptor('image', { location: '../data/profileImages', restrictions: { size: byte * 10 }, ...