Switch between underlining and not underlining matInput inside mat-form-field with a click

Is there a way to turn the underline on and off for a matInput within a mat-form-field using CSS or TypeScript?

I came across this post which demonstrates that the underline can be removed with CSS like this:

::ng-deep .mat-form-field-underline {
    display: none;
}

However, I am uncertain if it is possible to toggle this through an [ngClass] directive.

The post also mentions that it can be achieved programmatically, but I am not sure if this change can be reversed. Additionally, I am using material design with the prefix mat instead of md...

@ViewChild('input') input: MdInputDirective;

ngOnInit(){
  this.input.underlineRef.nativeElement.className = null;
}

You can view my example on StackBlitz.

Answer №1

To dynamically apply or remove a class in your component based on a condition (using boolean for example), you can utilize [ngClass] or [class.class-name].

The information provided above may be beneficial in addressing half of your concern. Additionally, the underline class is automatically assigned by Angular Material.

<div class="mat-form-field-underline"><span class="mat-form-field-ripple"></span></div>

An alternative solution to this problem is to include a class within the mat-form-field.

<mat-form-field [ngClass]="{'form-field--read':field.readOnly }" > <input matInput .........  /> </mat-form-field>

This approach can be enhanced with scss/css styling:

::ng-deep .form-field--read .mat-form-field-underline {
    display: none;
}

Answer №2

Consider experimenting with this approach

::ng-deep .mat-form-field-underline {
    Hide the display;
}

Answer №3

To enhance your mat-form-field, include the code snippet below:

[ngClass]="{'mat-form-field-invalid': errorExists}"

With errorExists being a boolean that indicates the presence of an error. If you already have an error variable, consider using something like "error != " instead of errorExists

Answer №4

To dynamically add or remove a class in your component based on a condition (using boolean as an example), you can utilize the [ngClass] directive or [class.class-name]. Check out the provided link for usage examples.

https://stackblitz.com/edit/angular-9w1w4b

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

Modifying the rotation speed of an 'fa-icon' element in Angular using Font Awesome

In my component, I am working with the following HTML template: ` <fa-icon class="basketball" [icon]="['fas','basketball-ball']" size="2x" [spin]="true"></fa-icon> ` This code generates the following HTML DOM element: ` & ...

Using camel case, format the data in json using gRPC

I have a json data from gRPC that is in Pascal case and I need to convert it to camel case. I am using angular to interact with the gRPC service. However, despite serializing and converting the result to camel case, the output in angular always remains in ...

JavaScript double-click functionality is not operational on IE9 and IE11 browsers

My JavaScript code has an issue where single-click opens a link in a new tab and double-click opens a lightbox. This works perfectly in all browsers except for IE9 and IE11. In my initial code, both single-click and double-click function correctly. However ...

Provide reasoning for sub bullet points using bullet points

Resolved! To resolve the issue, I modified the margin to 0 (instead of 0 auto) and included a left padding for each individual li element in my ordered list with type "a". Original Inquiry I currently have a list of numbered bullet points with type a ...

Tips for customizing the appearance of a checkbox made with AngularJS

I need to style the checkbox using CSS without modifying the HTML code provided due to company restrictions. Is there a way to customize the appearance of the checkbox purely through CSS? Below is the AngularJS code snippet: <label class="form-lbl n ...

What is the best way to eliminate the external pause button?

My website contains the following HTML snippet: <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="a ...

What steps can I take to address the issue of inconsistent vertical alignment of placeholder text on both desktop and mobile

The alignment of my input placeholder text varies across different browsers and devices. While it appears correctly in Firefox and Chrome desktop, Safari desktop and mobile, as well as Firefox Mobile and Chrome Mobile display the text too high. I've ...

Problem with adjusting the height of divs in CSS and HTML

Welcome to my JSFiddle project: https://jsfiddle.net/mkdjetkq/1/ I am currently working on developing a website that showcases a stunning region as part of a personal challenge. I have put in a lot of effort trying to consolidate everything I've acc ...

Button hyperlink

I'm working on a new 'tour' page that will showcase the dates for upcoming shows. I would like to include a button on the page that, when clicked, will display the number of available tickets and provide an option to purchase them. I am also ...

SharePoint 2013 on a mobile device does not support scrolling within iframes

Recently, I set up a SharePoint 2013 website with a few iframes. However, when I access the site on my mobile devices (iPad and Android), I am unable to scroll through the entire page by touching within the iframe. I attempted to solve this issue by inclu ...

Vue.js - Problem with loading image on screen

Although I have experience with React, I am currently facing the challenge of using Vue for a code assessment for the first time. My struggle lies in creating a reusable image component with WAI-ARIA accessibility. Despite my efforts, I cannot get the imag ...

Integrating content from a separate PHP page into the main index PHP page

I can't seem to get the #container1 div on my index.php page to load with the content from another #container2 div on page1.php. It's frustrating because I've checked my file hierarchy and everything seems correct. This is how my files are ...

A guide on utilizing the Check All feature in nglightning for Angular 2

Is there a feature available in nglightning that allows users to check all records in a datatable? You can find the component at the following link: I am specifically looking for the ability to check all checkboxes in nglightning within the header row. Is ...

EventEmitter does not properly capturing the event

I have been attempting to make my toolbar component send data to another component (dat.gui -> three.js). I thought creating an output emitter would be necessary, and while it is set up, it doesn't seem to be functioning properly. These components ...

Ensure that the sidebar in your React application occupies the full height of the page, regardless of the presence of scrolling

In my application, most of the pages fit within the viewport which is why I have been using height: 100vh for my <SideNav /> component. However, some pages now require scrolling and this causes issues with the sidebar as it abruptly ends when scrolli ...

Exploring the potential of utilizing Pipes in Angular with dynamically loaded components

I am facing a challenge where I need to dynamically load multiple components. To achieve this, I have set up a parent component to handle the task based on the value of this.rink, like so: ngAfterViewInit() { switch (this.rink) { case 'ITC ...

"Utilize CSS GRID without the need for ::before and ::after in grid layouts

While working with CSS Grid, I noticed that the grid also includes the before and after elements as grid items. Is there a way to address this issue? .wrapper { display: grid; grid-template-columns: 100px 100px 100px; grid-gap: 10px; background- ...

Tips for updating the chosen value with jquery or javascript

I am facing a situation where I need to change the selected option in a dropdown menu using a function triggered onClick later in the application. <select id="myselect"> <option value=aa>aa</option> <option value=bb>bb</option&g ...

Combining a JSON object with a dropdown menu within an Ionic 3 Angular 4 application

I've hit a roadblock while attempting to integrate a JSON response into an ion-option tag in my HTML code: <ion-item> <ion-label>Country</ion-label> <ion-select formControlName="country"> ...

Creating an event on the containing element

Here is my HTML tag: <ul> <li> <form>...</form> <div> <div class="A"></div> <div class="B"><img class="wantToShow"></div> </div> ...